欢迎光临
我们一直在努力

python中使用zip函数的七重境界

Python中有一些内置函数,可以使我们的代码非常优雅。​​zip​​ 函数就是其中之一,但是zip 函数的使用对于初学者来说不是很直观,有时容易出错。因此本文将从7个层次来由浅入深地来探讨强大的​​zip ​​函数的概念、用法和技巧。
了解zip函数基础语法

​​zip​​ 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的​​iterator​​。
举例如下,我们可以使用它以如下方式来组合两个列表,样例代码如下:

id = [1, 2, 3, 4]
leaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']
record = zip(id, leaders)

print(record)
# 

print(list(record))
# [(1, 'Elon Mask'), (2, 'Tim Cook'), (3, 'Bill Gates'), (4, 'Bai Li')]

如上面的示例所示,​​zip​​ 函数返回一个元组迭代器,其中第i 个元组包含每个列表中的第​​i ​​个元素。

zip函数同时处理多个对象

事实上,Python中的​​zip ​​函数具有强大的功能,比如它可以一次处理任意数量的可迭代项,而不仅仅是两个。

首先,我们来看如果我们将一个list 传递给​​ zip​​ 函数,样例如下:

id = [1, 2, 3, 4]
record = zip(id)
print(list(record))
# [(1,), (2,), (3,), (4,)]

如果我们同时传递三个​​list ​​,结果如下:

id = [1, 2, 3, 4]
leaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']
sex = ['m', 'm', 'm', 'm']
record = zip(id, leaders, sex)

print(list(record))
# [(1, 'Elon Mask', 'm'), (2, 'Tim Cook', 'm'), (3, 'Bill Gates', 'm'), (4, 'Bai Li', 'm')]

如上所述,无论我们传递给​​zip​​函数多少个可迭代项,它都能按照我们的预期来正常工作。
顺便说一下,如果没有参数,​​zip​​ 函数只返回一个空的迭代器。

zip函数处理长度不等的参数

真实数据并不总是干净和完整的,有时我们必须处理不等长的可迭代数据。默认情况下,​​zip​​函数的结果基于最短的可迭代项。
举例如下:

id = [1, 2]
leaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']
record = zip(id, leaders)

print(list(record))
# [(1, 'Elon Mask'), (2, 'Tim Cook')]

如上面的代码所示,最短的列表是​​id ​​,因此​​record​​ 只包含两个元组,并且忽略了列表​​leaders ​​中的最后两个元素。
如果最后两位​​leader​​ 因被忽视而不高兴,我们该怎么办?
Python将再次帮助我们。​​itertools ​​模块中还有一个名为​​zip_langest ​​的函数。顾名思义,它是​​zip​​函数的兄弟,其结果基于最长的参数。
我们不妨使用​​zip_langest ​​函数来生成上述​​record​​列表,结果如下:

from itertools import zip_longest
id = [1, 2]
leaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']

long_record = zip_longest(id, leaders)
print(list(long_record))
# [(1, 'Elon Mask'), (2, 'Tim Cook'), (None, 'Bill Gates'), (None, 'Bai Li')]

long_record_2 = zip_longest(id, leaders, fillvalue='Top')
print(list(long_record_2))
# [(1, 'Elon Mask'), (2, 'Tim Cook'), ('Top', 'Bill Gates'), ('Top', 'Bai Li')]

如上所述,​​zip_langest​​ 函数基于其最长参数来返回结果。可选的​​fillvalue​​ 参数(默认值为​​None​​ )可以帮助我们填充缺失的值。

掌握unzip操作

在上一个示例中,如果我们首先获得列表​​record​​ ,那么我们如何将其​​unzip​​ 解压缩为单独的可迭代项?
不幸的是,Python并没有直接的解压缩​​unzip ​​函数。然而,如果我们熟悉星号​​* ​​的技巧,解压缩将是一项非常简单的任务。

record = [(1, 'Elon Mask'), (2, 'Tim Cook'), (3, 'Bill Gates'), (4, 'Bai Li')]
id, leaders = zip(*record)
print(id)
# (1, 2, 3, 4)
print(leaders)
# ('Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li')

在上面的示例中,星号执行了拆包操作,即从记录列表中拆包所有四个元组。

通过zip函数创建和更新dict

受益于功能强大的​​zip​​ 函数,基于一些独立的列表来创建和更新dict 将非常方便。
我们可以使用以下​​one-line​​的方案:
● 使用字典生成式和zip函数
● 使用dict和zip函数
样例代码如下:

id = [1, 2, 3, 4]
leaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Bai Li']

# create dict by dict comprehension
leader_dict = {i: name for i, name in zip(id, leaders)}
print(leader_dict)
# {1: 'Elon Mask', 2: 'Tim Cook', 3: 'Bill Gates', 4:'Bai Li'}

# create dict by dict function
leader_dict_2 = dict(zip(id, leaders))
print(leader_dict_2)
# {1: 'Elon Mask', 2: 'Tim Cook', 3: 'Bill Gates', 4: 'Bai Li'}

# update
other_id = [5, 6]
other_leaders = ['Larry Page', 'Sergey Brin']
leader_dict.update(zip(other_id, other_leaders))
print(leader_dict)
# {1: 'Elon Mask', 2: 'Tim Cook', 3: 'Bill Gates', 4: ''Bai Li'', 5: 'Larry Page', 6: 'Sergey Brin'}

上面的示例根本不使用​​for​​ 循环 , 这是多么的优雅和​​Pythonic​​!

在for循环中使用zip函数

同时处理多个可迭代项通常是常见的场景, 此时我们可以在​​for​​循环中配合使用函数​​zip​​ ,这也是我最喜欢函数​​zip ​​的用法之一。
举例如下:

products = ["cherry", "strawberry", "banana"]
price = [2.5, 3, 5]
cost = [1, 1.5, 2]
for prod, p, c in zip(products, price, cost):
    print(f'The profit of a box of {prod} is £{p-c}!')
# The profit of a box of cherry is £1.5!
# The profit of a box of strawberry is £1.5!
# The profit of a box of banana is £3!
实现矩阵转置

我们来看以下问题:

如何优雅地实现矩阵的转置操作?

Wow, 鉴于在上文中我们已经介绍了函数​​zip​​ ,星号​​*​​ ,以及列表生成式,所以​​one-line​​ 的实现方式如下:

matrix = [[1, 2, 3], [1, 2, 3]]
matrix_T = [list(i) for i in zip(*matrix)]
print(matrix_T)
# [[1, 1], [2, 2], [3, 3]]
 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

除特别注明外,本站所有文章均基于CC-BY-NC-SA 4.0原创,转载请注明出处。
文章名称:《python中使用zip函数的七重境界》
文章链接:https://www.xpn.cc/5216/fy.html
分享到: 更多 (0)

热门推荐

评论 抢沙发

登录

忘记密码 ?