zip to unzip a dict is a very slow approach to do it Instead of mi = dict(zip(m.values(), m.keys())) Do mi = {v: k for (k, v) in m.iteritems()}
When did support for dictionary comprehensions make it into 2.x? I could've sworn it didn't used to work, but I just tried it in the shell and sure enough, it does in 2.7.3.
Python Language Features and Tricks
21–30 of 86 posts
Re: Python Language Features and Tricks
#22I know bugger all Python, but I know negative indexing.
Re: Python Language Features and Tricks
#23patterns / tricks = language deficiencies Wake me up when Python will support tail call elimination and will get rid of GIL. For now this language is no better than PHP.
[0] http://paulgraham.com/avg.html
[1] http://c2.com/cgi/wiki?BlubParadox
[2] http://weblog.raganwald.com/2006/10/are-we-blub-programmers....
Re: Python Language Features and Tricks
#24Re: Python Language Features and Tricks
#25Earlier quoted context omitted.
For TRE: http://neopythonic.blogspot.com/2009/04/tail-recursion-elimi... For the GIL, there are alternative implementations of Python (Jython, IronPython, etc.)
Hm... Python only supports 1000 recursions? That seems unsafe. It seems like anyone who writes functional-style code will run the risk of a stack overflow.
import sys
sys.setrecursionlimit(x)
Kind of a "hackish" way to do things but you can do it if you need to.
Re: Python Language Features and Tricks
#26I'll also think about the "collection of simple examples" next time I want to document something.
Re: Python Language Features and Tricks
#27I have two questions. 1. I'm unfamiliar with the term 'unpacking'. Is it any different from pattern matching in, say, Haskell (but perhaps not as feature-rich)? 2. Aren't slices pretty much a staple in Python? I didn't think using them was considered a 'trick'.
Unpacking is a limited form of what is called destructuring in other languages like Clojure. I would say that, in terms of feature-richness: unpacking < destructuring < pattern matching.
unpacking
where first-class patterns are increasingly becoming available in some languages which offer pattern matching (in particular, Haskell will have them soon).Re: Python Language Features and Tricks
#28I believe in 2.7 zip is still returning a list rather than an iterator (izip in Python 2, zip in Python 3+).
Another unappreciated stdlib is definitely functools. mock is also another awesome stdlib.
functools, collections and itertools are definitely useful to make things faster. Also check out the list of stdlib. http://docs.python.org/2/library/
Re: Python Language Features and Tricks
#29zip to unzip a dict is a very slow approach to do it Instead of mi = dict(zip(m.values(), m.keys())) Do mi = {v: k for (k, v) in m.iteritems()}
Re: Python Language Features and Tricks
#301.29 happened to be exactly what I was looking for:
for subset in itertools.chain(*(itertools.combinations(a, n) for n in range(len(a) + 1)))
I spent way too much time writing a function to come up with these combinations.