【发布时间】:2018-09-06 06:39:19
【问题描述】:
我有两个列表。一个带有名称,一个带有与第一个列表中的名称对应的数字(对应的名称和数字在每个列表中的同一索引点)。我需要在一次只能处理 25 个不同名称和点的 url 中引用每个名称和数字。
pointNames = ['name1', 'name2', 'name3']
points = ['1', '2', '3'] #yes, the numbers are meant to be strings
我的实际列表中每个列表大约有 600 个值。我想要做的是同时循环遍历每个列表,但以 25 为增量。我可以使用以下方法对单个列表执行此操作:
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
for group in chunker(pointNames, 25):
print (group)
这会从列表中打印多组 25 个值,直到它遍历整个列表。我想这样做,但有两个列表。我可以使用 for(point, name) in zip(points, pointNames): 完全打印每个列表,但我需要 25 个一组。
我也尝试过将这两个列表组合成一个字典:
dictionary = dict(zip(points, pointNames))
for group in chunker(dictionary, 25):
print (group)
但我收到以下错误:
TypeError: unhashable type: 'slice'
【问题讨论】:
标签: python python-3.x list loops dictionary