Python Language Features and Tricks
31–40 of 86 posts
Re: Python Language Features and Tricks
#32zip 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()}
more importantly it's wrong - the iteration order of .values() and .keys() is not guaranteed to be consistent
Re: Python Language Features and Tricks
#33Re: Python Language Features and Tricks
#34Earlier quoted context omitted.
I think unpacking is just unpacking, languages that have pattern matching allow you to do something identical: # let i = (1,2,3) ;; val i : int * int * int = (1, 2, 3) # let a,b,c = i;; val a : int = 1 val b : int = 2 val c : int = 3
Unpacking pretty much does one thing, and it makes that one thing easier and a lot more readable. Combine that with list comprehension (from what I understand C#'s LINQ is similar) and you end up with code that's highly maintainable/readable (as long as you name your variables appropriately, of course). I believe Python supports pattern matching other than Regex as well.
> I believe Python supports pattern matching other than Regex as well.
That's pattern matching on strings. The kind of pattern matching being discussed here is pattern matching on data types (see [1]).
Re: Python Language Features and Tricks
#35Earlier quoted context omitted.
Unpacking pretty much does one thing, and it makes that one thing easier and a lot more readable. Combine that with list comprehension (from what I understand C#'s LINQ is similar) and you end up with code that's highly maintainable/readable (as long as you name your variables appropriately, of course). I believe Python supports pattern matching other than Regex as well.
Python list comprehensions are not lazy, though. If you generate [for x in xrange(1, 10000)] you'll get 10000 elements in your list. My understanding is that LINQ list comprehensions are that. > I believe Python supports pattern matching other than Regex as well. That's pattern matching on strings. The kind of pattern matching being discussed here is pattern matching on data types (see [1]). 1: http://en.wikipedia.or…
Re: Python Language Features and Tricks
#36Earlier quoted context omitted.
Unpacking pretty much does one thing, and it makes that one thing easier and a lot more readable. Combine that with list comprehension (from what I understand C#'s LINQ is similar) and you end up with code that's highly maintainable/readable (as long as you name your variables appropriately, of course). I believe Python supports pattern matching other than Regex as well.
Python list comprehensions are not lazy, though. If you generate [for x in xrange(1, 10000)] you'll get 10000 elements in your list. My understanding is that LINQ list comprehensions are that. > I believe Python supports pattern matching other than Regex as well. That's pattern matching on strings. The kind of pattern matching being discussed here is pattern matching on data types (see [1]). 1: http://en.wikipedia.or…
It doesnt have functional style pattern matching though.
Re: Python Language Features and Tricks
#37I don't understand how this one to flatten lists works:
a = [[1, 2], [3, 4], [5, 6]]
[x for l in a for x in l]
Can somebody explain what the order of operations is here and what the variables refer to in the various stages of evaluation?Re: Python Language Features and Tricks
#38Earlier quoted context omitted.
Python list comprehensions are not lazy, though. If you generate [for x in xrange(1, 10000)] you'll get 10000 elements in your list. My understanding is that LINQ list comprehensions are that. > I believe Python supports pattern matching other than Regex as well. That's pattern matching on strings. The kind of pattern matching being discussed here is pattern matching on data types (see [1]). 1: http://en.wikipedia.or…
'Lazy lists' are called generators in Python. Just use (...) Instead of [...] in your example.
Re: Python Language Features and Tricks
#39I think this is great, I've been doing Python for a while and I knew many of the features but I also learned a few new ones. I don't understand how this one to flatten lists works: a = [[1, 2], [3, 4], [5, 6]] [x for l in a for x in l] Can somebody explain what the order of operations is here and what the variables refer to in the various stages of evaluation?
result = []
for l in a:
for x in l:
result.append(x)Re: Python Language Features and Tricks
#40Earlier quoted context omitted.
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.
You can increase the recursion limit like this: import sys sys.setrecursionlimit(x) Kind of a "hackish" way to do things but you can do it if you need to.
For example, 1000 levels of recursion is more than enough to count all the nodes in a binary tree unless the tree is extremely poorly balanced or inordinately large.