Python で zip

Linda Hamilton
Linda Hamiltonオリジナル
2024-12-28 16:54:17752ブラウズ

zip in Python

コーヒー買ってきて☕

zip() は、以下に示すように複数の反復可能オブジェクトを組み合わせて反復可能オブジェクトを作成できます。
*メモ:

  • 反復可能は、最も短い入力反復可能がなくなると停止します。
  • 反復可能オブジェクトにはインデックスを使用して直接アクセスできないため、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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。