python 内建函数 python3
python zip
names = ['anne', 'beth', 'george', 'damon']
ages = [12, 45, 32, 102]
for i in range(len(names)):
print(names[i], 'is', ages[i], 'years old')
输出
anne is 12 years old
beth is 45 years old
george is 32 years old
damon is 102 years old
代码:
zipped = zip(names, ages)
print(zipped)
list(zipped)
输出:
<zip object at 0x0000000005186348>
[('anne', 12), ('beth', 45), ('george', 32), ('damon', 102)]
代码:
for name ,age in zip(names, ages):
print(name, 'is', age, 'years old')
输出:
anne is 12 years old
beth is 45 years old
george is 32 years old
damon is 102 years old
本文通过实例演示了Python中zip()函数的使用方法,展示了如何将多个列表打包成元组列表,以及如何利用zip()函数简化for循环遍历多个列表的操作。

被折叠的 条评论
为什么被折叠?



