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.
。。。。。等等