python内置函数 Z

python内置函数 Z

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

Z

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

zip(*iterables, strict=False)

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

示例:

>>> 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() 在最短的迭代完成后停止。较长可迭代对象中的剩余项将被忽略,结果会裁切至最短可迭代对象的长度:

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

    >>> list(zip(('a', 'b', 'c'), (1, 2, 3), strict=True))
    [('a', 1), ('b', 2), ('c', 3)]
    

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

    >>> 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 参数。

# 创建两个列表  
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 文档

相关推荐

  1. python函数 Z

    2024-04-03 04:06:01       16 阅读
  2. Python 函数

    2024-04-03 04:06:01       18 阅读
  3. 详解Python函数 !!!

    2024-04-03 04:06:01       36 阅读
  4. python函数 L

    2024-04-03 04:06:01       19 阅读
  5. python函数 O

    2024-04-03 04:06:01       18 阅读
  6. python函数 T

    2024-04-03 04:06:01       20 阅读
  7. python函数 V

    2024-04-03 04:06:01       20 阅读
  8. python函数 V

    2024-04-03 04:06:01       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-03 04:06:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-03 04:06:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-03 04:06:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-03 04:06:01       20 阅读

热门阅读

  1. Nginx-记

    Nginx-记

    2024-04-03 04:06:01      14 阅读
  2. 第7单元日考

    2024-04-03 04:06:01       17 阅读
  3. LeetCode104.二叉树的最大深度

    2024-04-03 04:06:01       14 阅读
  4. mysql 存储过程示例

    2024-04-03 04:06:01       18 阅读
  5. 以下哪个变量不是指针类型

    2024-04-03 04:06:01       16 阅读
  6. LeetCode-41. 缺失的第一个正数【数组 哈希表】

    2024-04-03 04:06:01       16 阅读
  7. nginx输出日志配置与查看

    2024-04-03 04:06:01       17 阅读
  8. 论微服务架构及应用

    2024-04-03 04:06:01       14 阅读
  9. Memcached 教程之 Memcached replace 命令(七)

    2024-04-03 04:06:01       17 阅读