美文网首页
py3笔记11:zip

py3笔记11:zip

作者: _百草_ | 来源:发表于2023-05-23 10:55 被阅读0次

zip(*iterable)

  • 用于将可迭代的对象作为参数,将对象在的对应元素打包成一个个元组,然后返回由这些元组组成的对象
  • 节约内存
  • 若各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同
  • 返回迭代器,使用list()输出列表(py2返回一个列表)
"""
    zip(*iterables) --> A zip object yielding tuples until an input is exhausted.
    
       >>> list(zip('abcdefg', range(3), range(4)))
       [('a', 0, 0), ('b', 1, 1), ('c', 2, 2)]
    
    The zip object yields n-length tuples, where n is the number of iterables
    passed as positional arguments to zip().  The i-th element in every tuple
    comes from the i-th iterable argument to zip().  This continues until the
    shortest argument is exhausted.
"""
alist = [1,2,3]
s = "abcdef"
st_dict = {"name":"bai","age":18}
zip_list = zip(alist,s,st_dict)  # zip object
print(list(zip_list))  # [(1, 'a', 'name'), (2, 'b', 'age')]
print()

相关文章

网友评论

      本文标题:py3笔记11:zip

      本文链接:https://www.haomeiwen.com/subject/hhdxedtx.html