1, zip(可迭代对象1, 可迭代对象2...)
from collections.abc import Iterator my_list_1 = [1, 2, 3, 4] my_list_2 = [6, 7, 8] result = zip(my_list_1, my_list_2) # 该迭代器返回tuple print(type(result)) # <class 'zip'> print(isinstance(result, Iterator)) # True for i_tuple in result: print(i_tuple, type(i_tuple))
结果:
(1, 6) <class 'tuple'>
(2, 7) <class 'tuple'>
(3, 8) <class 'tuple'>
from collections.abc import Iterator my_list_1 = [1, 2, 3, 4] my_list_2 = [6, 7, 8] result = zip(my_list_1, my_list_2) # 该迭代器返回tuple print(type(result)) # <class 'zip'> print(isinstance(result, Iterator)) # True for i, j in result: print(i, type(i), j, type(j))
结果:
1 <class 'int'> 6 <class 'int'>
2 <class 'int'> 7 <class 'int'>
3 <class 'int'> 8 <class 'int'>
zip+*Iterator
用1个参数a接收,得到的就是zip对象
my_list_1 = [1, 2, 3, 4] my_list_2 = [6, 7, 8] result = zip(my_list_1, my_list_2) # 该迭代器返回tuple a = zip(*result) # 等同于zip((1, 6), (2, 7), (3, 8)) print(type(a)) # <class 'zip'>
用匹配的参数接收(这边是2个),就是实际元素
my_list_1 = [1, 2, 3, 4] my_list_2 = [6, 7, 8] result = zip(my_list_1, my_list_2) # 该迭代器返回tuple # *result: 展开迭代器, 拿到3个tuple, (1, 6), (2, 7), (3, 8) a, b = zip(*result) # 等同于zip((1, 6), (2, 7), (3, 8)) print(a, type(a), b, type(a)) # (1, 2, 3) <class 'tuple'> (6, 7, 8) <class 'tuple'>
标签:内置,zip,tuple,python,list,result,print,my From: https://www.cnblogs.com/sailJs/p/17677141.html