Pythonic way to combine for-loop and if-statement
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Question
00:55 Accepted answer (Score 437)
01:10 Answer 2 (Score 49)
02:53 Answer 3 (Score 33)
03:23 Answer 4 (Score 24)
03:59 Thank you
--
Full question
https://stackoverflow.com/questions/6981...
Accepted answer links:
[generator expressions]: https://www.python.org/dev/peps/pep-0289
Answer 2 links:
[The Zen of Python]: http://www.python.org/dev/peps/pep-0020/
[sorted]: http://www.python.org/doc//current/libra...
[intersection]: http://www.python.org/doc//current/libra...
[set]: http://www.python.org/doc//current/libra...
[generator expression]: http://www.python.org/doc//current/tutor...
[enumerate]: http://www.python.org/doc//current/libra...
Answer 4 links:
https://docs.python.org/2/library/operat...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #loops #ifstatement #forloop
#avk47
ACCEPTED ANSWER
Score 456
You can use generator expressions like this:
gen = (x for x in xyz if x not in a)
for x in gen:
print(x)
ANSWER 2
Score 50
As per The Zen of Python (if you are wondering whether your code is "Pythonic", that's the place to go):
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Flat is better than nested.
- Readability counts.
The Pythonic way of getting the sorted intersection of two sets is:
>>> sorted(set(a).intersection(xyz))
[0, 4, 6, 7, 9]
Or those elements that are xyz but not in a:
>>> sorted(set(xyz).difference(a))
[12, 242]
But for a more complicated loop you may want to flatten it by iterating over a well-named generator expression and/or calling out to a well-named function. Trying to fit everything on one line is rarely "Pythonic".
Update following additional comments on your question and the accepted answer
I'm not sure what you are trying to do with enumerate, but if a is a dictionary, you probably want to use the keys, like this:
>>> a = {
... 2: 'Turtle Doves',
... 3: 'French Hens',
... 4: 'Colly Birds',
... 5: 'Gold Rings',
... 6: 'Geese-a-Laying',
... 7: 'Swans-a-Swimming',
... 8: 'Maids-a-Milking',
... 9: 'Ladies Dancing',
... 0: 'Camel Books',
... }
>>>
>>> xyz = [0, 12, 4, 6, 242, 7, 9]
>>>
>>> known_things = sorted(set(a.iterkeys()).intersection(xyz))
>>> unknown_things = sorted(set(xyz).difference(a.iterkeys()))
>>>
>>> for thing in known_things:
... print 'I know about', a[thing]
...
I know about Camel Books
I know about Colly Birds
I know about Geese-a-Laying
I know about Swans-a-Swimming
I know about Ladies Dancing
>>> print '...but...'
...but...
>>>
>>> for thing in unknown_things:
... print "I don't know what happened on the {0}th day of Christmas".format(thing)
...
I don't know what happened on the 12th day of Christmas
I don't know what happened on the 242th day of Christmas
ANSWER 3
Score 25
I personally think this is the prettiest version:
a = [2,3,4,5,6,7,8,9,0]
xyz = [0,12,4,6,242,7,9]
for x in filter(lambda w: w in a, xyz):
print x
Edit
if you are very keen on avoiding to use lambda you can use partial function application and use the operator module (that provides functions of most operators).
https://docs.python.org/2/library/operator.html#module-operator
from operator import contains
from functools import partial
print(list(filter(partial(contains, a), xyz)))
ANSWER 4
Score 19
I would probably use:
for x in xyz:
if x not in a:
print(x...)