zip()函数的用法及疑惑
cate = ['客户A', '客户B', '客户C', '客户D', '客户E', '其他客户']
data = [153, 124, 107, 99, 89, 46]
x = [list(z) for z in zip(cate, data)] #列表
y = [z for z in zip(cate, data)] #元祖
print(x)
print(type(x)) # <class 'list'>
print(y)
print(type(y)) # <class 'list'>
print(x==y) # False
输出结果:
[['客户A', 153], ['客户B', 124], ['客户C', 107], ['客户D', 99], ['客户E', 89], ['其他客户', 46]]
<class 'list'>
[('客户A', 153), ('客户B', 124), ('客户C', 107), ('客户D', 99), ('客户E', 89), ('其他客户', 46)]
<class 'list'>
False
同时
m = zip(cate, data)
# <zip object at 0x00000000038D3B40> 输出对象的内存位置而不打印出来
print(m)
print(list(m))
print(list(m)==y)
输出
zip object at 0x0000000003883500>
[('客户A', 153), ('客户B', 124), ('客户C', 107), ('客户D', 99), ('客户E', 89), ('其他客户', 46)]
<class 'list'>
False
为什么list(m)和y不相同??输出一致,类型也一致!print(list(m)==y)却是False!
n = [zip(cate,data)]
print(n) # [<zip object at 0x00000000038D3A40>]
print(type(n)) # <class 'list'>
print(list(n))
print(type(list(n))) # <class 'list'>
print(list(n)==n) # True
输出:
[<zip object at 0x00000000038D9980>]
<class 'list'>
[<zip object at 0x00000000038D9980>]
<class 'list'>
True