[英]Splitting two lists and assigning data from one list to values in the other
[英]Splitting a list into two seperate lists, by every other item in python
您好,我有一个快速的问题,我似乎无法解决。
我有一个清单:
a = [item1, item2, item3, item4, item5, item6]
我想通过其他所有项目将此列表分为两个单独的列表,例如:
b = [item1, item3, item5]
c = [item2, item4, item6]
使用切片,指定一个步骤:
b,c = a[::2], a[1::2]
使用过滤器是一种选择:
a = [item1, item2, item3, item4, item5, item6]
b = filter(lambda x: a.index(x) % 2 == 0, a)
c = filter(lambda x: a.index(x) % 2 != 0, a)
编辑:这将要求元素是唯一的并且效率低下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.