python学习日记4,zip()的使用,数据类型tuple list及使用

test_X=[1,2,3,4,5]
test_Y=[6,7,8,9,10]#list类型
test_zipxy=zip(test_X,test_Y)
print(‘test_zipxy:’,test_zipxy)#zip类型

test_tuple = tuple(test_zipxy)#从zip转化为tuple成功
#print(‘test_tuple:’,test_tuple)

#test_list = list(test_zipxy)
#zip转化为list成功,但转成tuple还是list只用用一个,后面的会被干扰成失败
#print(‘test_list:’,test_list)

#(zipx,zipy)=zip(*test_zipxy)
#print(‘zipx:’,zipx)
#分解成两个tuple成功,但和前面的list() tuple()互相干扰,三个只能用一个

for (x,y) in test_tuple:
#for (x,y) in test_list:
#for (x,y) in zip(test_X,test_Y):
print(‘tuple:x,y:’,x,y)

print(‘zipx:’,zipx)
print(‘test_x:’,test_X)

test_zipxy: <zip object at 0x0000000010085608>
tuple:x,y: 1 6
tuple:x,y: 2 7
tuple:x,y: 3 8
tuple:x,y: 4 9
tuple:x,y: 5 10
zipx: (1, 2, 3, 4, 5)
test_x: [1, 2, 3, 4, 5]

如果写成
for x in (zipx,zipy):
print(x)
结果是

(1, 2, 3, 4, 5)
(6, 7, 8, 9, 10)

更多参考资料
https://docs.python.org/3/library/functions.html#zip

在这里插入图片描述

zip(*iterables)
Make an iterator(迭代器) that aggregates elements(聚合整合元素) from each of the iterables(容器类型如 list,str,tuple,dict,file).

Returns an iterator of tuples元组, where the i-th tuple contains the i-th element from each of the argument(实际变量) sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. Equivalent to:

def zip(*iterables):
# zip(‘ABCD’, ‘xy’) --> Ax By
sentinel = object()
iterators = [iter(it) for it in iterables]
while iterators:
result = []
for it in iterators:
elem = next(it, sentinel)
if elem is sentinel:
return
result.append(elem)
yield tuple(result)

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). This repeats the same iterator n times so that each output tuple has the result of n calls to the iterator. This has the effect of dividing the input into n-length chunks.

zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use itertools.zip_longest() instead.

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

》 x = [1, 2, 3]
》 y = [4, 5, 6]
》 zipped = zip(x, y)
》 list(zipped)
[(1, 4), (2, 5), (3, 6)]
》 x2, y2 = zip(*zip(x, y))
》 x == list(x2) and y == list(y2)
True

test_X=[1,2,3,4,5]
test_Y=[6,7,8,9,10]
test_zipxy=zip(test_X,test_Y)
print(test_zipxy)
list(test_zipxy)
执行结果:
<zip object at 0x000000000C1F4A88>
Out[36]: [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

type(test_zipxy)的结果就是
zip

Tuple不可变,用()
list可变,用[]
zip命令返回的是指向tuple的iterator

https://docs.python.org/3/library/stdtypes.html#typesseq
Sequence Types — list, tuple, range
There are three basic sequence types: lists, tuples, and range objects. Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections.

Common Sequence Operations
The operations in the following table are supported by most sequence types, both mutable and immutable. The collections.abc.Sequence ABC is provided to make it easier to correctly implement these operations on custom sequence types.

This table lists the sequence operations sorted in ascending priority. In the table, s and t are sequences of the same type, n, i, j and k are integers and x is an arbitrary object that meets any type and value restrictions imposed by s.

。。。。。等等

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值