我有以下for和if条件,for循环后跟if条件, 关于如何将它们组合成一行的任何建议?
for x in ids:
if x!=12345
答案 0 :(得分:4)
for x in (i for i in ids if i!=12345):
# do stuff
In [37]: ids
Out[37]: [12343, 12344, 12345, 12346, 12347, 12348]
In [38]: for x in (i for i in ids if i!=12345):
....: print x
....:
12343
12344
12346
12347
12348