我试图做到这一点,以便列表中的所有偶数都分配给一个变量,而每个奇数都分配给另一个变量。例如,假设x = ["a", "b", "c", "d"]
。我将如何制作y = ["a", "c"]
和z = ["b", "d"]
?
我还没有编写任何脚本,但是将来我会
答案 0 :(得分:5)
您的意思是:
>>> y, z = x[::2], x[1::2]
>>> y
['a', 'c']
>>> z
['b', 'd']
>>>
答案 1 :(得分:0)
x=['a','b','c','d']
y[:]=x[::2] #start from 0 index value : till the end of list : step (every second element)
z[:]=x[1::2] #start from 1 index value : till the end of list : step (every second element)
答案 2 :(得分:-1)
a = [i for i in x if x.index(i)%2==0]
b = [i for i in x if x.index(i)%2!=0]