上下文将整数列表拆分为自己的偶数和奇数列表。
even = []
odd = []
for i in my_list:
if i % 2 == 0:
even.append(i)
else:
odd.append(i)
有没有办法将上面变成一个漂亮,紧凑的列表理解...?
答案 0 :(得分:5)
不使用副作用并丢弃结果。你可以这样做:
even = []
odd = []
for i in my_list:
(odd if i % 2 else even).append(i)
这个问题一般称为分区列表,您可以通过搜索SO找到some solutions,但没有一个更清晰(在Python中)。
答案 1 :(得分:4)
不是真的,你可以使用副作用破解一些东西,但这不是列表理解的内容
>>> even = []
>>> odd = []
>>> [(odd if i%2 else even).append(i) for i in range(10)]
[None, None, None, None, None, None, None, None, None, None] # it's a waste to make this list
>>> even
[0, 2, 4, 6, 8]
>>> odd
[1, 3, 5, 7, 9]
稍微浪费(但更难理解)就是这个
>>> even = []
>>> odd = [i for i in range(10) if i%2 or even.append(i)]
>>> even
[0, 2, 4, 6, 8]
>>> odd
[1, 3, 5, 7, 9]
但是,您可以使用第一个列表推导中的条件来简化循环
even = []
odd = []
for i in my_list: # Doesn't make a pointless list of `None`
(odd if i%2 else even).append(i)
如果my_list
真的很长,可能值得将append方法绑定到局部变量以保存额外的查找(为10000的列表节省~30%)
even = []
odd = []
even_append = even.append
odd_append = odd.append
for i in my_list:
(odd_append if i%2 else even_append)(i)
另一种加速是使用i&1
代替i%2
选择偶数或奇数
答案 2 :(得分:3)
如果奇数/偶数匹配列表索引,您可以使用切片来执行此操作:
>>> my_list=list(range(20))
>>> even,odd=my_list[0::2],my_list[1::2]
>>> even
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> odd
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
如果你只想要紧凑(和快速):
>>> isodd=lambda x: x%2
>>> random.shuffle(my_list)
>>> even,odd=[x for x in my_list if not isodd(x)],[x for x in my_list if isodd(x)]
>>> even
[12, 6, 2, 18, 14, 0, 10, 16, 8, 4]
>>> odd
[17, 1, 19, 11, 15, 5, 9, 13, 7, 3]
答案 3 :(得分:1)
单程:
my_list=[i for i in range(50)]
lists=[[], []]
[ lists[x % 2].append(x) for x in my_list ]
print lists[0]
print lists[1]
答案 4 :(得分:1)
不是世界上最短(或最好)的列表理解,但至少它是无副作用的,它是在不使用append()
的情况下以函数式编程风格编写的。你走了:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even, odd = [[x for x in y if x is not None] for y in zip(*((None, e) if e % 2 else (e, None) for e in lst))]
even
=> [2, 4, 6, 8, 10]
odd
=> [1, 3, 5, 7, 9]