Python zip() 函数详解(Detailed explanation of Python zip() function)

  • python3.x的定义与python2.x定义的主要区别是返回值,在python3.x为了减少内存,返回的是一个zip对象,可以用list、dict等进行转换。
  • 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表(也叫减包运算)。
  • iterabl — 一个或多个可迭代对象
  • iterable,… 表示多个列表、元组、字典、集合、字符串,甚至还可以为 range() 区间
  • iterable中可以为不同的数据类型
  • 多个迭代器之间用逗号隔开
  • 返回元组列表(python2.x)
  • 返回zip对象(其实这里就是一个迭代器)(python3.x)
list_3 = [11, 12, 13, 14, 15]
print(next(list_3))
执行结果:
TypeError: 'list' object is not an iterator
  • 同一数据类型
list_1 = [1, "str", 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [11, 12, 13, 14, 15]
result = list(zip(list_1, list_2, list_3))
print(result)

执行结果:
[(1, 6, 11), ('str', 7, 12), (3, 8, 13), (4, 9, 14), (5, 10, 15)]
  • 不同数据类型
list_1 = [1, "str", 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [11, 12, 13, 14, 15]
dict_4 = {"张三": 18, "王五": 19, "赵四": 18, "王琦": 19, "王虎": 18, "张六": 19}
tuple_5 = {"a", "b", "c", "d", "e"}
set_6 = {20, 30, 40, 50, 60}

result = list(zip(list_1, list_2, list_3, dict_4, tuple_5,set_6))
print(result)


执行结果:
[(1, 6, 11, '张三', 'b', 40), ('str', 7, 12, '王五', 'd', 50), (3, 8, 13, '赵四', 'c', 20), (4, 9, 14, '王琦', 'a', 60), (5, 10, 15, '王虎', 'e', 30)]
  • 不同长度的同一数据类型
list_1 = [1, "str", 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [11, 12]
result = list(zip(list_1, list_2, list_3))
print(result)
执行结果:
[(1, 6, 11), ('str', 7, 12)]
  • 不同长度的不同类型
list_1 = [1, "str", 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [11, 12]
dict_4 = {"张三": 18, "王五": 19, "赵四": 18, "王琦": 19, "王虎": 18, "张六": 19}
tuple_5 = {"a", "b", "c", "d", "e"}
set_6 = {20, 30, 40}

result = list(zip(list_1, list_2, list_3, dict_4,tuple_5,set_6))
print(result)

执行结果:
[(1, 6, 11, '张三', 'b', 40), ('str', 7, 12, '王五', 'e', 20)]

转载至:https://mp.weixin.qq.com/s/FR4qAUB4wULw4g3abhUmAA

————————
  • python3. Definition of X and python2 The main difference in the definition of X is the return value, which is in Python 3 In order to reduce memory, X returns a zip object, which can be converted with list, dict, etc.
  • If the number of elements of each iterator is inconsistent, the length of the returned list is the same as that of the shortest object. The * operator can decompress the tuple into a list (also known as subtraction operation).
  • Iterabl — one or more iteratable objects
  • iterable,… Represents multiple lists, tuples, dictionaries, sets, strings, and even ranges ()
  • There can be different data types in Iterable
  • Multiple iterators are separated by commas
  • Return tuple list (python2. X)
  • Return the zip object (in fact, this is an iterator) (Python 3. X)
list_3 = [11, 12, 13, 14, 15]
print(next(list_3))
执行结果:
TypeError: 'list' object is not an iterator
  • Same data type
list_1 = [1, "str", 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [11, 12, 13, 14, 15]
result = list(zip(list_1, list_2, list_3))
print(result)

执行结果:
[(1, 6, 11), ('str', 7, 12), (3, 8, 13), (4, 9, 14), (5, 10, 15)]
  • Different data types
list_1 = [1, "str", 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [11, 12, 13, 14, 15]
dict_4 = {"张三": 18, "王五": 19, "赵四": 18, "王琦": 19, "王虎": 18, "张六": 19}
tuple_5 = {"a", "b", "c", "d", "e"}
set_6 = {20, 30, 40, 50, 60}

result = list(zip(list_1, list_2, list_3, dict_4, tuple_5,set_6))
print(result)


执行结果:
[(1, 6, 11, '张三', 'b', 40), ('str', 7, 12, '王五', 'd', 50), (3, 8, 13, '赵四', 'c', 20), (4, 9, 14, '王琦', 'a', 60), (5, 10, 15, '王虎', 'e', 30)]
  • Same data type with different length
list_1 = [1, "str", 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [11, 12]
result = list(zip(list_1, list_2, list_3))
print(result)
执行结果:
[(1, 6, 11), ('str', 7, 12)]
  • Different types of different lengths
list_1 = [1, "str", 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [11, 12]
dict_4 = {"张三": 18, "王五": 19, "赵四": 18, "王琦": 19, "王虎": 18, "张六": 19}
tuple_5 = {"a", "b", "c", "d", "e"}
set_6 = {20, 30, 40}

result = list(zip(list_1, list_2, list_3, dict_4,tuple_5,set_6))
print(result)

执行结果:
[(1, 6, 11, '张三', 'b', 40), ('str', 7, 12, '王五', 'e', 20)]

转载至:https://mp.weixin.qq.com/s/FR4qAUB4wULw4g3abhUmAA