4 ms·
I'm not so sure if set notation is higher level, but for the example in your longer comment, map/filter/product is not that bad if you use it wisely. Here is my
by riobard 17y ago
I'm not so sure if set notation is higher level, but for the example in your longer comment, map/filter/product is not that bad if you use it wisely. Here is my version:
map(lambda (w,s,l): {'widget': w, 'sprocket': s, 'location': l}
filter(lambda (w,s,l): l.hasInStock(w) and l.hasInStock(s) and w.isUsableWith(s),
product(widgets, sprockets, locations)))
Well, I agree it is not any conciser that its list comprehension (about the same I guess?). Nothing is perfect, like you said, know you tool :)
- frig 17y agoYou'll be thrilled (lol) to know that the lambda-tuple syntax isn't in python 3 (!); it does make my examples more concise. The argument in favor of set-notation being higher level is it's less specific (it doesn't explicitly provide a sequence of operations, just an outcome). List comprehensions look like set notation but have an implicit procedural translation you have to keep in mind to use them well, so it's a toss-up. I prefer map/filter/reduce when sequencing has large performance implications but for simple filtering or raw-data-shaping comprehensions read more smoothly. http://books.google.com/books?id=RvY5BM0Xt1wC&lpg=PT367&ots=m_Yzk8XnDQ&dq=python%20lambda%20tuple%20argument&pg=PT367#v=onepage&q=python%20lambda%20tuple%20argument&f=false http://books.google.com/books?id=RvY5BM0Xt1wC&lpg=PT367&...
- riobard 17y ago> You'll be thrilled (lol) to know that the lambda-tuple syntax isn't in python 3 Now you know why many people like me are gradually pissed off by Python and start the exile ... currently trying Scala and it seems a nice language (with list comprehension too! :) > List comprehensions look like set notation but have an implicit procedural translation you have to keep in mind to use them well, so it's a toss-up. Actually I think that's the problem I have with list comprehensions: I use them a lot in my code, usually 1~3 levels nested and then have a hard time tracking down the order of implicit loops (which is inner vs outer) and make sure the intermediate variables (x for x in y for y in z ...) do not clash ... OK maybe I'm using it too much and in the wrong way :( > I prefer map/filter/reduce when sequencing has large performance implications but for simple filtering or raw-data-shaping comprehensions read more smoothly. I didn't know map/filter is faster than list comprehensions? I thought both are optimized by Python interpreter. But I like the idea of knowing that at least map can be parallelized easily. But since Python does not utilize multicore in a decent way, all bets are off :(