Re: or else...aklepatcNovember 4 2015, 01:11:23 UTC
It is_good: ) I am in love with list/set/dict comprehensions. So everything looks like a nail ;) For a more technical point compare dis results for this:
def foo(it, bad): return {x for x in it if is_good(x) or bad.add(x)} and that:
def bar(it, bad): good = set() for x in it: (good if is_good(x) else bad).add(x) return good You will be impressed by the difference.
Comments 2
good = set()
bad=set()
def is_good(n): return (n % 3) == 0
for s in source:
(good if is_good(s) else bad).add(s)
print "good = " + str(good)
print "bad = " + str(bad)
Reply
I am in love with list/set/dict comprehensions. So everything looks like a nail ;)
For a more technical point compare dis results for this:
def foo(it, bad):
return {x for x in it if is_good(x) or bad.add(x)}
and that:
def bar(it, bad):
good = set()
for x in it:
(good if is_good(x) else bad).add(x)
return good
You will be impressed by the difference.
Reply
Leave a comment