问题:您有多个具有相应元素的可迭代对象(如列表或元组),您想按元素顺序将它们组合成对或元组。

解决方案: 您可以使用 Python 中的 zip() 函数将多个可迭代对象合并成一个可迭代对象,并将对应的元素分组。

解决方案如下:

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

# Using zip() to combine the lists
zipped = zip(list1, list2)

# Convert the zip object to a list (or another iterable)
result = list(zipped)

print(result) # [(1, 'a'), (2, 'b'), (3, 'c')]

使用 zip() 函数