前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python文档研读系列:zip函数

Python文档研读系列:zip函数

作者头像
Python中文社区
发布2018-01-31 12:57:42
4910
发布2018-01-31 12:57:42
举报
文章被收录于专栏:Python中文社区Python中文社区

zip([iterable, ...])

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).

zip() in conjunction with the * operator can be used to unzip a list:

Formerly, zip() required at least one argument and zip() raised a TypeError instead of returning an empty list.

zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。利用*号操作符,可以将list unzip(解压)

zip函数接受任意多个序列作为参数,将所有序列按相同的索引组合成一个元素是各个序列合并成的tuple的新序列,新的序列的长度以参数中最短的序列为准。另外(*)操作符与zip函数配合可以实现与zip相反的功能,即将合并的序列拆成多个tuple。

①tuple的新序列

②新的序列的长度以参数中最短的序列为准.

③(*)操作符与zip函数配合可以实现与zip相反的功能,即将合并的序列拆成多个tuple

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-10-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python中文社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • zip([iterable, ...])
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档