Python의 zip

Linda Hamilton
Linda Hamilton원래의
2024-12-28 16:54:17748검색

zip in Python

커피 사주세요주의!

zip()은 아래와 같이 여러 iterable을 결합하여 iterable을 생성할 수 있습니다.
*메모:

  • 가장 짧은 입력 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.