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