[英]How would I make every other thing in a list in a different variable list?
I am trying to make it so that every even thing in a list goes to one variable, and every odd one goes to another. 我试图做到这一点,以使列表中的所有偶数都分配给一个变量,而每个奇数都分配给另一个变量。 For example, let's say x = ["a", "b", "c", "d"]
. 例如,假设x = ["a", "b", "c", "d"]
。 How would I make y = ["a", "c"]
and z = ["b", "d"]
? 我如何使y = ["a", "c"]
和z = ["b", "d"]
?
I have not made any script with this yet, but I will in the future 我尚未为此编写任何脚本,但将来会
You mean: 你的意思是:
>>> y, z = x[::2], x[1::2]
>>> y
['a', 'c']
>>> z
['b', 'd']
>>>
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)
a = [i for i in x if x.index(i)%2==0]
b = [i for i in x if x.index(i)%2!=0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.