python内置函数 Z

python内置函数 Z

Python 解释器内置了很多函数和类型,任何时候都能使用。

Z

名称 描述
zip 返回元组的迭代器。

zip (*iterables , strict=False)

在多个迭代器上并行迭代,从每个迭代器返回一个数据项组成元组。

示例:

python 复制代码
>>> for item in zip([1, 2, 3], ['sugar', 'spice', 'everything nice']):
...     print(item)
...
(1, 'sugar')
(2, 'spice')
(3, 'everything nice')

更正式的说法: zip() 返回元组的迭代器,其中第 i 个元组包含的是每个参数迭代器的第 i 个元素。

不妨换一种方式认识 zip() :它会把行变成列,把列变成行。这类似于 矩阵转置

zip() 是延迟执行的:直至迭代时才会对元素进行处理,比如 for 循环或放入 list 中。

值得考虑的是,传给 zip() 的可迭代对象可能长度不同;有时是有意为之,有时是因为准备这些对象的代码存在错误。Python 提供了三种不同的处理方案:

  • 默认情况下,zip() 在最短的迭代完成后停止。较长可迭代对象中的剩余项将被忽略,结果会裁切至最短可迭代对象的长度:

    python 复制代码
    >>> list(zip(range(3), ['fee', 'fi', 'fo', 'fum']))
    [(0, 'fee'), (1, 'fi'), (2, 'fo')]
  • 通常 zip() 用于可迭代对象等长的情况下。这时建议用 strict=True 的选项。输出与普通的 zip() 相同:

    python 复制代码
    >>> list(zip(('a', 'b', 'c'), (1, 2, 3), strict=True))
    [('a', 1), ('b', 2), ('c', 3)]

    与默认行为不同,如果一个可迭代对象在其他几个之前被耗尽则会引发 ValueError:

    python 复制代码
    >>> for item in zip(range(3), ['fee', 'fi', 'fo', 'fum'], strict=True):  
    ...     print(item)
    ...
    (0, 'fee')
    (1, 'fi')
    (2, 'fo')
    Traceback (most recent call last):
      ...
    ValueError: zip() argument 2 is longer than argument 1

    如果未指定 strict=True 参数,所有导致可迭代对象长度不同的错误都会被抑制,这可能会在程序的其他地方表现为难以发现的错误。

  • 为了让所有的可迭代对象具有相同的长度,长度较短的可用常量进行填充。这可由 itertools.zip_longest() 来完成。

极端例子是只有一个可迭代对象参数,zip() 会返回一个一元组的迭代器。如果未给出参数,则返回一个空的迭代器。

小技巧:

  • 可确保迭代器的求值顺序是从左到右的。这样就能用 zip(*[iter(s)]*n, strict=True) 将数据列表按长度 n 进行分组。这将重复 相同 的迭代器 n 次,输出的每个元组都包含 n 次调用迭代器的结果。这样做的效果是把输入拆分为长度为 n 的块。

  • zip()* 运算符相结合可以用来拆解一个列表:

    复制代码
    >>> x = [1, 2, 3]
    >>> y = [4, 5, 6]
    >>> list(zip(x, y))
    [(1, 4), (2, 5), (3, 6)]
    >>> x2, y2 = zip(*zip(x, y))
    >>> x == list(x2) and y == list(y2)
    True

在 3.10 版本发生变更: 增加了 strict 参数。

python 复制代码
# 创建两个列表  
list1 = [1, 2, 3]  
list2 = ['a', 'b', 'c']  
  
# 使用zip()将两个列表的元素配对  
zipped = zip(list1, list2)  
  
# 将zip对象转换为列表以便查看其内容  
zipped_list = list(zipped)  
print(zipped_list)  # 输出: [(1, 'a'), (2, 'b'), (3, 'c')]  
  
# 如果两个列表长度不同,zip()将停止在最短的列表结束时  
list3 = [4, 5, 6, 7]  
zipped_unequal = zip(list1, list3)  
zipped_unequal_list = list(zipped_unequal)  
print(zipped_unequal_list)  # 输出: [(1, 4), (2, 5), (3, 6)] 注意,7没有被包括进来  
  
# zip()也可以用于三个或更多列表  
list4 = ['x', 'y', 'z']  
zipped_three = zip(list1, list2, list4)  
zipped_three_list = list(zipped_three)  
print(zipped_three_list)  # 输出: [(1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z')]

# zip()可以与 * 运算符一起使用来解压缩(unzip)元组列表
zipped_list = [(1, 'a'), (2, 'b'), (3, 'c')]
list1, list2 = zip(*zipped_list)
print(list(list1))  # 输出: (1, 2, 3)
print(list(list2))  # 输出: ('a', 'b', 'c')

zip() 返回的是一个迭代器,这意味着你只能遍历它一次。如果你需要多次访问其内容,你需要将其转换为列表或其他可迭代对象。

参考:内置函数 --- Python 3.12.2 文档

相关推荐
CodeCraft Studio2 小时前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
零点零一2 小时前
VS+QT的编程开发工作:关于QT VS tools的使用 qt的官方帮助
开发语言·qt
困鲲鲲4 小时前
Python中内置装饰器
python
摩羯座-185690305944 小时前
Python数据可视化基础:使用Matplotlib绘制图表
大数据·python·信息可视化·matplotlib
lingchen19065 小时前
MATLAB的数值计算(三)曲线拟合与插值
开发语言·matlab
爱隐身的官人5 小时前
cfshow-web入门-php特性
python·php·ctf
gb42152875 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
THMAIL5 小时前
量化股票从贫穷到财务自由之路 - 零基础搭建Python量化环境:Anaconda、Jupyter实战指南
linux·人工智能·python·深度学习·机器学习·金融
~-~%%5 小时前
从PyTorch到ONNX:模型部署性能提升
人工智能·pytorch·python
一朵梨花压海棠go5 小时前
html+js实现表格本地筛选
开发语言·javascript·html·ecmascript