How do I access the index while iterating over a sequence with a for loop? Desired output: Answer Use the built-in function enumerate(): It is non-pythonic to manually index via for i in range(len(xs)): x = xs[i] or manually manage an additional state variable. Check out PEP 279 for more.
Tag: python
How to clear the interpreter console?
Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc. Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same comma…
What is the best way to remove accents (normalize) in a Python unicode string?
I have a Unicode string in Python, and I would like to remove all the accents (diacritics). I found on the web an elegant way to do this (in Java): convert the Unicode string to its long normalized form (with a separate character for letters and diacritics) remove all the characters whose Unicode type is R…
Multidimensional array in Python
I have a little Java problem I want to translate to Python. Therefor I need a multidimensional array. In Java it looks like: Further values will be created bei loops and written into the array. How do I instantiate the array? PS: There is no matrix multiplication involved… Answer You can create it using…
Why is it string.join(list) instead of list.join(string)?
This has always confused me. It seems like this would be nicer: Than this: Is there a specific reason it is like this? Answer It’s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the “joiner” must be strings. For example: Using something other than stri…
Timeout on a function call
I’m calling a function in Python which I know may stall and force me to restart the script. How do I call the function or what do I wrap it in so that if it takes longer than 5 seconds the script cancels it and does something else? Answer You may use the signal package if you are running on
Convert list of ints to one number?
I have a list of integers that I would like to convert to one number like: What is the best way to implement the magic function? EDIT I did find this, but it seems like there has to be a better way. Answer
Locking a file in Python
I need to lock a file for writing in Python. It will be accessed from multiple Python processes at once. I have found some solutions online, but most fail for my purposes as they are often only Unix based or Windows based. Answer Alright, so I ended up going with the code I wrote here, on my website link is
How do I remove duplicates from a list, while preserving order?
How do I remove duplicates from a list, while preserving order? Using a set to remove duplicates destroys the original order. Is there a built-in or a Pythonic idiom? Answer Here you have some alternatives: http://www.peterbe.com/plog/uniqifiers-benchmark Fastest one: Why assign seen.add to seen_add instead o…
How do I simulate flip of biased coin?
In unbiased coin flip H or T occurs 50% of times. But I want to simulate coin which gives H with probability ‘p’ and T with probability ‘(1-p)’. something like this: Answer random.random() returns a uniformly distributed pseudo-random floating point number in the range [0, 1). This num…