Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a big file with some format. I want to substitute some items in the string by others, the string is long (like a big XML but with no format).
I know where they are, i could locate them by using a regular expression for each, but i wonder which is the best method, easier and better if its the most efficient way.
format/% already searches the string for parameter placeholders internally. Since they're implemented in C, you're not gonna beat their performance with Python code even if your search and replace workload is somewhat simpler. See Faster alternatives to numpy.argmax/argmin which is slow for a glance on C to Python relative performance.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Here is the operation that I want to perform:
for sample in samples:
if sample['something'] is None:
sample['something'] = 'something_else'
I want to write this in a more elegant and Pythonic way. It would be great if some experts in Python could help. samples is a list of dictionaries.
for sample in (elt for elt in samples if elt["something"] is None):
sample["something"] = "something_else"
Note use of generator expression to not build another in-memory list, though this might be unnecessary.
I also would not use explicit None check either, unless empty collections, strings or 0 should be considered "truthy" values -- often they aren't, with the exception of zero. if not foo reads IMO better than if foo is not None. If this is your case too, you could do just
for sample in samples:
sample["something"] = sample["something"] or "something_else"
Then again I wouldn't probably bother, original would be good enough for me, and like suggested in comments, using or shortcut could be a tad hacky (in a bad way) for some readers.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
If i have 2 algorithms A and B
Where A uses O(n logn) time and O(1) extra space.
On the other hand B uses O(nk) time and O(k-1) extra space.
What you guys think which one would be a better choice? And why?
Better choice is the one which doesn't exceed memory budget and is measurably faster in production conditions. A reliable way to find that out is measuring.
If you're writing a library that will/may be used in many different environments, then a good choice is to implement both algorithms and let the user choose.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Although this is a question of maths but I really wanted to figure out if something like this is possible or not by coding it .(preferably in C). I had the question posted in mathsstackexchange where I saw that there's a way to find out the cosets of D_12 in S_6 by using Python programming. Can someone just help me to figure out how are we doing this?
S_n is the group formed by all possible permutations of n-elements and D_2n is formed by the generators <r, s> where r =(123, n) and s=(1n)(2 n-1).
Here's the link to the answer
https://math.stackexchange.com/questions/3880306/find-the-cosets-of-d-2n-in-s-n
Also I am not accustomed to posting questions in stackoverflow, I really don't know how to add mathematical symbols.
Though the logic remains same, the implementation will vary from Python to c due to difference is data types and so on. So you should try learning Python, it’s very easy to learn and you can pick up writing code in Python within few days considering you already you know a few other languages. And writing such complex programs can be easy in Python due to the vast inbuilt libraries and readability. So it’s better if you learn Python and start implementing this in Python.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
When using re.search or re.match, is it faster to use .lower() or re.IGNORECASE?
By faster I mean which takes the least time to execute?
Also is one more pythonic than the other? - This is potentially opinionated, but I would like to know for my own knowledge, the most important thing is which executes faster when the script is run.
I.E
mystring = "TeSt"
lowerresults = re.match("^[a-z]{4}$", mystring.lower())
ignoreresults = re.match("^[a-z]{4}$", mystring, re.IGNORECASE)
Edit:
The case of the output is not important, speed and matching the regex I have built is all I am concerned about.
Generally the re.IGNORECASE is a better solution overall.
Speedwise, .lower() has to transform the string, potentially taking up more time. Also, the resulting matches will be lowercase. In case you want to retain the case in the result, this won't work. Also, space wise you will have to store the lowercase string as well.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to develop a piece of code that converts a number to the corresponding words, e.g. 1 -> "One", 2 -> "Two"
Is there any function in Python to do this task?
The answer to this question is "no". There is no function in Python to do this task.
If you "have to develop code to do it" (your words), then using a builtin wouldn't really be a valid solution, perhaps?
If you have to develop code to do it, you need better specifications. Do you have to be able to just do 0..9, or any cardinal number, or any number at all? (floating point? decimal? negative?). Why do you have to develop this code? Is it homework, or some special purpose?
If you just have to do 0..9, then as mentioned in comments, you should use a dictionary. Take care of case of the input.
If you have to do anything more than that, looking at the implementation of num2word would certainly be educational.