概述
说明:
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。
语法:
zip(iterable1,iterable2, ...)
参数:
iterable -- 一个或多个可迭代对象(字符串、列表、元祖、字典)
返回值:
Python2中直接返回一个由元组组成的列表,Python3中返回的是一个对象,如果想要得到列表,可以用 list() 函数进行转换。
实例:
函数功能是聚合传入的每个迭代器中相同位置的元素,返回一个新的元组类型迭代器。
In [5]: l = [1,2,3]
In [6]: ll = [4,5,6]
In [7]: zip_l =zip(l,ll)
In [8]: from collections importIterable
In [9]: isinstance(zip_l,Iterable)
Out[9]: True
In [10]: for j,k inzip_l:
....:print(j,k)
....:1 4
2 5
3 6In [11]: list(zip_l)
Out[11]: [] # 返回的是迭代器。
如果传入的迭代器长度不一致,最短长度的迭代器迭代结束后停止聚合。
In [22]: l_s = [1,2,3] # 长度为3
In [23]: l_l = [4,5,6,7] # 长度为4
In [24]: list(zip(l_s,l_l))
Out[24]: [(1, 4), (2, 5), (3, 6)] # 输出的值是以长度比较短的值进行的。
如果只传入一个迭代器,则返回的单个元素元组的迭代器。
In [25]: list(zip([1,2,3]))
Out[25]: [(1,), (2,), (3,)]
如果不传入参数,则返回空的迭代器。
In [26]: list(zip())
Out[26]: []
zip(*[iter(s)]*n)等效于调用zip(iter(s),iter(s),...,iter(s))。
In [28]: l = [1,2,3]
In [29]: ll = l*3In [30]: ll
Out[30]: [1, 2, 3, 1, 2, 3, 1, 2, 3]
In [31]: lll = [l]*3In [32]: lll
Out[32]: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
In [33]: list(zip(*[l]*3))
Out[33]: [(1, 1, 1), (2, 2, 2), (3, 3, 3)]
In [34]: list(zip(l,l,l))
Out[34]: [(1, 1, 1), (2, 2, 2), (3, 3, 3)]
与 zip 相反,*zip 可理解为解压,返回二维矩阵式
In [35]: l = [1,2,3]
In [36]: ll = [4,5,6]
In [37]: zip(l,ll)
Out[37]: In [38]: list(zip(l,ll))
Out[38]: [(1, 4), (2, 5), (3, 6)]
In [39]: zip(*zip(l,ll))
Out[39]: In [40]: list(zip(*zip(l,ll)))
Out[40]: [(1, 2, 3), (4, 5, 6)]
In [41]: a,b = list(zip(*zip(l,ll)))
In [42]: a
Out[42]: (1, 2, 3)
In [43]: b
Out[43]: (4, 5, 6)
In [44]: list(a)
Out[44]: [1, 2, 3]
In [45]: list(b)
Out[45]: [4, 5, 6]
zip的高级应用
转换二维数组
In [20]: ll = [[1,2,3],[4,5,6],[7,8,9]]
In [21]: [ [row[col] for row in ll] for col inrange(len(ll[0]))]
Out[21]: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
使用zip(*)进行转换
In [22]: ll = [[1,2,3],[4,5,6],[7,8,9]]
In [23]: zip(*ll)
Out[23]: In [24]: list(zip(*ll))
Out[24]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
In [25]: map(list,zip(*ll))
Out[25]: In [26]: list(map(list,zip(*ll)))
Out[26]: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
使用zip合并相邻的列表项
In [42]: a = [1, 2, 3, 4, 5, 6]
In [43]: zip(*([iter(a)] * 2))
Out[43]: In [44]: list(zip(*([iter(a)] * 2)))
Out[44]: [(1, 2), (3, 4), (5, 6)]
合并列表式的一个推导式
In [71]: group_adjacent = lambda a, k: zip(*([iter(a)] *k))
In [72]: group_adjacent(a, 3)
Out[72]: In [73]: list(group_adjacent(a, 3))
Out[73]: [(1, 2, 3), (4, 5, 6)]
In [74]: list(group_adjacent(a, 2))
Out[74]: [(1, 2), (3, 4), (5, 6)]
In [75]: list(group_adjacent(a, 1))
Out[75]: [(1,), (2,), (3,), (4,), (5,), (6,)]
--------
In [76]: zip(a[::2], a[1::2])
Out[76]: In [77]: list(zip(a[::2], a[1::2]))
Out[77]: [(1, 2), (3, 4), (5, 6)]
In [78]: zip(a[::3], a[1::3], a[2::3])
Out[78]: In [79]: list(zip(a[::3], a[1::3], a[2::3]))
Out[79]: [(1, 2, 3), (4, 5, 6)]
-------
In [80]: group_adjacent = lambda a, k: zip(*(a[i::k] for i inrange(k)))
In [81]: list(group_adjacent(a, 3))
Out[81]: [(1, 2, 3), (4, 5, 6)]
In [82]: list(group_adjacent(a, 2))
Out[82]: [(1, 2), (3, 4), (5, 6)]
In [83]: list(group_adjacent(a, 1))
Out[83]: [(1,), (2,), (3,), (4,), (5,), (6,)]
使用zip和iterators生成滑动窗口 (n -grams)
In [85]: defn_grams(a, n):
....: z= (islice(a, i, None) for i inrange(n))
....:return zip(*z)
....:
In [86]: a = [1, 2, 3, 4, 5, 6]
In [87]: list(n_grams(a, 3))
Out[87]: [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]
In [88]: list(n_grams(a, 2))
Out[88]: [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
In [89]: list(n_grams(a, 1))
Out[89]: [(1,), (2,), (3,), (4,), (5,), (6,)]
In [90]: list(n_grams(a, 4))
Out[90]: [(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]
使用zip反转字典
In [91]: m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
In [92]: m.items()
Out[92]: dict_items([('d', 4), ('c', 3), ('b', 2), ('a', 1)])
In [93]: zip(m.values(), m.keys())
Out[93]: In [94]: list(zip(m.values(), m.keys())
In [94]: list(zip(m.values(), m.keys()))
Out[94]: [(4, 'd'), (3, 'c'), (2, 'b'), (1, 'a')]
In [95]: mi =dict(zip(m.values(), m.keys()))
In [96]: mi
Out[96]: {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
最后
以上就是自然天空为你收集整理的python重写内置函数_python 内置函数的全部内容,希望文章能够帮你解决python重写内置函数_python 内置函数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复