我正在努力想出一个有效而简单的解决方案:
我有两个dicts列表:
list_dicts_1 = [
{"name": "Suarez", "footed": "right-footed", "color": "black"}
{"name": "Suarez2", "footed": "right-footed2", "color": "black2"}
{"name": "Suarez3", "footed": "right-footed3", "color": "black3"}
{"name": "Suarez4", "footed": "right-footed4", "color": "black4"}
{"name": "Suarez5", "footed": "right-footed5", "color": "black5"}
{"name": "Suarez6", "footed": "right-footed6", "color": "black6"}
]
list_dicts_2 = [
{"name": "Coutinho", "footed": "left-footed", "color": "orange"}
{"name": "Coutinho2", "footed": "left-footed1", "color": "orange2"}
{"name": "Coutinho3", "footed": "left-footed2", "color": "orange3"}
{"name": "Coutinho4", "footed": "left-footed4", "color": "orange4"}
{"name": "Coutinho5", "footed": "left-footed5", "color": "orange5"}
{"name": "Coutinho6", "footed": "left-footed6", "color": "orange6"}
]
我想迭代这些dicts列表并将它们分配给3个空列表:
list_1 = []
list_2 = []
list_3 = []
期望的输出:
list_1 = [
{"name": "Suarez", "footed": "right-footed", "color": "black"},
{"name": "Suarez4", "footed": "right-footed4", "color": "black4"},
{"name": "Coutinho", "footed": "left-footed", "color": "orange"},
{"name": "Coutinho4", "footed": "left-footed4", "color": "orange4"}
]
list_2 = [
{"name": "Suarez2", "footed": "right-footed2", "color": "black2"},
{"name": "Suarez5", "footed": "right-footed5", "color": "black5"},
{"name": "Coutinho2", "footed": "left-footed2", "color": "orange2"},
{"name": "Coutinho5", "footed": "left-footed5", "color": "orange5"}
]
list_3 = [
{"name": "Suarez3", "footed": "right-footed3", "color": "black3"},
{"name": "Suarez6", "footed": "right-footed6", "color": "black6"},
{"name": "Coutinho3", "footed": "left-footed3", "color": "orange3"},
{"name": "Coutinho6", "footed": "left-footed6", "color": "orange6"}
]
我想在3个空列表中平均分配dicts列表。 dicts列表中的每个项目只能在空列表中一次。所以dicts列表的第一行应该放在list_1中。那么dicts列表的第二行应该放在list_2等等中,直到dicts列表中没有任何内容。
有没有简单的方法来实现这一目标?
答案 0 :(得分:5)
迭代器在这里非常有用:你可以使用itertools.cycle
来获得一个无休止地遍历list_1
,list_2
和list_3
的迭代器。然后简单地遍历list_dicts_1
和list_dicts_2
中的词组,并将它们附加到您从cycle
获得的列表中:
import itertools
list_1, list_2, list_3 = lists = [], [], []
# make an iterator that endlessly loops over list_1, list_2 and list_3
itr = itertools.cycle(lists)
# loop over the dicts
for dic in itertools.chain(list_dicts_1, list_dicts_2):
# and append the dict to the next list
next(itr).append(dic)
我使用itertools.chain
将list_dicts_1
和list_dicts_2
合并到一个可迭代中,并使用next
函数手动推进cycle
迭代器。
结果:
>>> list_1
[{'name': 'Suarez', 'footed': 'right-footed', 'color': 'black'},
{'name': 'Suarez4', 'footed': 'right-footed4', 'color': 'black4'},
{'name': 'Coutinho', 'footed': 'left-footed', 'color': 'orange'},
{'name': 'Coutinho4', 'footed': 'left-footed4', 'color': 'orange4'}]
>>> list_2
[{'name': 'Suarez2', 'footed': 'right-footed2', 'color': 'black2'},
{'name': 'Suarez5', 'footed': 'right-footed5', 'color': 'black5'},
{'name': 'Coutinho2', 'footed': 'left-footed1', 'color': 'orange2'},
{'name': 'Coutinho5', 'footed': 'left-footed5', 'color': 'orange5'}]
>>> list_3
[{'name': 'Suarez3', 'footed': 'right-footed3', 'color': 'black3'},
{'name': 'Suarez6', 'footed': 'right-footed6', 'color': 'black6'},
{'name': 'Coutinho3', 'footed': 'left-footed2', 'color': 'orange3'},
{'name': 'Coutinho6', 'footed': 'left-footed6', 'color': 'orange6'}]
另一种选择是将两个输入列表连接成一个巨大的列表,然后将其切片:
list_ = list_dicts_1 + list_dicts_2
list_1 = list_[::3]
list_2 = list_[1::3]
list_3 = list_[2::3]
但是,连接列表有点昂贵,所以如果列表非常大,应该避免使用。
答案 1 :(得分:2)
您可以使用while
循环来执行此操作:
while True:
try:
list_1.append(dicts[0])
del dicts[0]
list_2.append(dicts[0])
del dicts[0]
list_3.append(dicts[0])
del dicts[0]
except IndexError:
break
修改强>
也可以使用for
循环来完成!
for i in range(len(dicts)//3):
list_1.append(dicts[0])
del dicts[0]
list_1.append(dicts[0])
del dicts[0]
list_1.append(dicts[0])
del dicts[0]
编辑2:
它可以做得更好,而且对于一个人来说仍然很容易理解
for i in range(len(dicts)//3):
list_1.append(dicts[i*3])
list_1.append(dicts[i*3+1])
list_1.append(dicts[i*3+2])