不曾见过的Python函数——zip()
2017-03-22 17:04
351 查看
zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。利用*号操作符,可以将list
unzip(解压)
>>> x=[1,2,3] >>> y=[4,5,6] >>> zipped=zip(x,y) >>> zipped <zip object at 0x0000026687053D88> >>> list(zipped) [(1, 4), (2, 5), (3, 6)] >>> list(zip(*zipped)) [(1, 2, 3), (4, 5, 6)] >>> x_ylist=[] >>> for a,b in zip(x,y): dict={'x':a,'y':b} x_ylist.append(dict) >>> x_ylist [{'x': 1, 'y': 4}, {'x': 2, 'y': 5}, {'x': 3, 'y': 6}] >>> for a,b in zip(x,y): dict={'x':a,'y':b} print(dict) {'x': 1, 'y': 4} {'x': 2, 'y': 5} {'x': 3, 'y': 6} >>> x.append(9) >>> x [1, 2, 3, 9] >>> y [4, 5, 6] >>> list(zip(x,y)) [(1, 4), (2, 5), (3, 6)]
相关文章推荐
- 不曾见过的Python函数——extend()
- 不曾见过的Python函数——startswith(),endswith()
- 不曾见过的Python函数——pprint.pprint()
- 不曾见过的Python函数——map()
- 不曾见过的Python函数——any(),all()
- 不曾见过的Python函数——join()
- Python的zip函数
- python中lambda函数 list comprehension 和 zip函数使用指南
- Python中zip()函数用法举例[转载]
- Python中zip()函数用法举例
- Python的zip函数
- python zip函数同时遍历两个数组和构造字典
- 【转】python zip函数说明
- Python中的zip函数使用示例
- Python中zip()函数用法举例
- python中的函数:zip ,map,enumerate
- [转]Python中zip()函数用法举例
- Python中zip函数用法
- Python中zip()函数用法实例教程