zip en Python

Linda Hamilton
Linda Hamiltonoriginal
2024-12-28 16:54:17745parcourir

zip in Python

Achetez-moi un café☕

zip() peut créer un itérable en combinant plusieurs itérables comme indiqué ci-dessous :
*Mémos :

  • L'itérable s'arrête lorsque l'itérable d'entrée le plus court est épuisé.
  • L'itérable n'est pas accessible directement avec index, utilisez donc list() pour y accéder avec index.
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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn