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

1. 引言

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

2. Level 0: 了解zip函数基础语法

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

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

print(record)
# <zip object at 0x7f266a707d80>

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

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

3. Level 1: 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 函数只返回一个空的迭代器。

4. Level 2: 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 )可以帮助我们填充缺失的值。

5. Level 3: 掌握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')

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

6. Level 4: 通过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

7. Level 5: 在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!

8. Level 6: 实现矩阵转置

我们来看以下问题:

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

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]]

9. 总结

本文重点介绍了Python中功能强大的zip 函数的多种用法,并给出了相应的代码示例。
在这里插入图片描述

您学废了嘛?

  • 11
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵卓不凡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值