首页 > 后端开发 > Python教程 > Python 中的 zip

Python 中的 zip

Linda Hamilton
发布: 2024-12-28 16:54:17
原创
748 人浏览过

zip in Python

请我喝杯咖啡☕

zip() 可以通过组合多个 iterable 来创建一个 iterable,如下所示:
*备注:

  • 当最短的输入迭代耗尽时,迭代停止。
  • iterable 不能直接用索引访问,所以使用 list() 通过索引访问它。
fruits = ["Apple", "Orange", "Banana", "Kiwi", "Lemon", "Mango"]
meats = ["Chicken", "Beef", "Pork", "Duck", "Mutton"]
vegetables = ["Onion", "Carrot", "Garlic", "Spinach", "Eggplant"]

print(zip(fruits, meats, vegetables))
# <zip object at 0x7da5876aaa40>

print(zip(fruits, meats, vegetables)[0])
# Error

print(list(zip(fruits, meats, vegetables)))
# [('Apple', 'Chicken', 'Onion'),
#  ('Orange', 'Beef', 'Carrot'),
#  ('Banana', 'Pork', 'Garlic'),
#  ('Kiwi', 'Duck', 'Spinach'),
#  ('Lemon', 'Mutton', 'Eggplant')]

f, m, v = list(zip(fruits, meats, vegetables))[0]
print(f, m, v)
# Apple Chicken Onion

for f, m, v in zip(fruits, meats, vegetables):
    print(f, m, v)
# Apple Chicken Onion
# Orange Beef Carrot
# Banana Pork Garlic
# Kiwi Duck Spinach
# Lemon Mutton Eggplant
登录后复制
fruits = ["Apple", "Orange", "Banana", "Kiwi", "Lemon", "Mango"]
meats = ["Chicken", "Beef", "Pork", "Duck", "Mutton"]
vegetables = ["Onion", "Carrot", "Garlic", "Spinach", "Eggplant"]

print(list(zip(zip(fruits, meats), vegetables)))
# [(('Apple', 'Chicken'), 'Onion'),
#  (('Orange', 'Beef'), 'Carrot'),
#  (('Banana', 'Pork'), 'Garlic'),
#  (('Kiwi', 'Duck'), 'Spinach'),
#  (('Lemon', 'Mutton'), 'Eggplant')]

fm, v = list(zip(zip(fruits, meats), vegetables))[0]
print(fm, v)
# ('Apple', 'Chicken') Onion

(f, m), v = list(zip(zip(fruits, meats), vegetables))[0]
[f, m], v = list(zip(zip(fruits, meats), vegetables))[0]
print(f, m, v)
# Apple Chicken Onion

for fm, v in zip(zip(fruits, meats), vegetables):
    print(fm, v)
# ('Apple', 'Chicken') Onion
# ('Orange', 'Beef') Carrot
# ('Banana', 'Pork') Garlic
# ('Kiwi', 'Duck') Spinach
# ('Lemon', 'Mutton') Eggplant

for (f, m), v in zip(zip(fruits, meats), vegetables):
for [f, m], v in zip(zip(fruits, meats), vegetables):
    print(f, m, v)
# Apple Chicken Onion
# Orange Beef Carrot
# Banana Pork Garlic
# Kiwi Duck Spinach
# Lemon Mutton Eggplant
登录后复制
fruits = ["Apple", "Orange", "Banana", "Kiwi", "Lemon", "Mango"]
meats = ["Chicken", "Beef", "Pork", "Duck", "Mutton"]
vegetables = ["Onion", "Carrot", "Garlic", "Spinach", "Eggplant"]

print(list(zip(zip(fruits, zip(meats)), vegetables)))
# [(('Apple', ('Chicken',)), 'Onion'),
#  (('Orange', ('Beef',)), 'Carrot'),
#  (('Banana', ('Pork',)), 'Garlic'),
#  (('Kiwi', ('Duck',)), 'Spinach'),
#  (('Lemon', ('Mutton',)), 'Eggplant')]

fm, v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
print(fm, v)
# ('Apple', ('Chicken',)) Onion

(f, m), v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
[f, m], v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
print(f, m, v)
# Apple ('Chicken',) Onion

(f, (m,)), v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
[f, [m]], v = list(zip(zip(fruits, zip(meats)), vegetables))[0]
print(f, m, v)
# Apple Chicken Onion

for fm, v in zip(zip(fruits, zip(meats)), vegetables):
    print(fm, v)
# ('Apple', ('Chicken',)) Onion
# ('Orange', ('Beef',)) Carrot
# ('Banana', ('Pork',)) Garlic
# ('Kiwi', ('Duck',)) Spinach
# ('Lemon', ('Mutton',)) Eggplant

for (f, m), v in zip(zip(fruits, zip(meats)), vegetables):
for [f, m], v in zip(zip(fruits, zip(meats)), vegetables):
    print(f, m, v)
# Apple ('Chicken',) Onion
# Orange ('Beef',) Carrot
# Banana ('Pork',) Garlic
# Kiwi ('Duck',) Spinach
# Lemon ('Mutton',) Eggplant

for (f, (m,)), v in zip(zip(fruits, zip(meats)), vegetables):
for [f, [m]], v in zip(zip(fruits, zip(meats)), vegetables):
    print(f, m, v)
# Apple Chicken Onion
# Orange Beef Carrot
# Banana Pork Garlic
# Kiwi Duck Spinach
# Lemon Mutton Eggplant
登录后复制

以上是Python 中的 zip的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板