Python 中的 zip
请我喝杯咖啡☕
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中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章
Windows 11 KB5054979中的新功能以及如何解决更新问题
3 周前
By DDD
如何修复KB5055523无法在Windows 11中安装?
3 周前
By DDD
Inzoi:如何申请学校和大学
4 周前
By DDD
如何修复KB5055518无法在Windows 10中安装?
3 周前
By DDD
在哪里可以找到Atomfall中的站点办公室钥匙
4 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

攻克Investing.com的反爬虫策略许多人尝试爬取Investing.com(https://cn.investing.com/news/latest-news)的新闻数据时,常常�...
