python zip()与zip(*ziped)以及list(zip(a,b))


#环境python3.7
>>> a = [0,0,1,2,4,5,6,7,5] >>> b = [0,0,1,2,4,5,6,7,5] >>> zip(a,b) <zip object at 0x08C049E0> #我们会发现:zip(a,b)返回一个 object,试着解压缩吧 >>> c = zip(*zip(a,b)) >>> c <zip object at 0x08C04A58> #啊?解压缩也是个object,只是后面的地址不一样了!真的解压出来了吗?让我试试 >>> (g,h) = zip(a,b) Traceback (most recent call last): File "<pyshell#19>", line 1, in <module> (g,h) = zip(a,b) ValueError: too many values to unpack (expected 2) #压缩之后是看不了的!那么解压缩后呢? >>> (e,f) = c >>> (e,f) ((0, 0, 1, 2, 4, 5, 6, 7, 5), (0, 0, 1, 2, 4, 5, 6, 7, 5)) #可以确定就是解压缩

我们会发现不是像python2中那样了!!!

 参考地址:https://www.runoob.com/python/python-func-zip.html

 

a = [1,2,3,4,5,6,7]

b = [1,2,3,4,5,6,7]

print(zip(a,b))
for (i,j) in zip(a,b):
    print(i,j)
print(list(zip(a,b)))
for (i,j) in list(zip(a,b)):
    print(i,j)

#一直不明白,为什么明明zip(a,b)和list(zip(a,b)不一样,但是for循环结果一样的

#可能就类似于这个吧!
a = range(5)
print(a)

for i in a:
    print(i)

'''
输出:
<zip object at 0x031AE6E8>
1 1
2 2
3 3
4 4
5 5
6 6
7 7
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)]
1 1
2 2
3 3
4 4
5 5
6 6
7 7
range(0, 5)
0
1
2
3
4
>>> 

'''

 

list(zip()) 之后元素的操作,参考博文:https://blog.csdn.net/wangxingfan316/article/details/81387652

posted @ 2020-03-28 19:14  落月_YU  阅读(2371)  评论(0编辑  收藏  举报