list_3 = [11, 12, 13, 14, 15] print(next(list_3)) 执行结果: TypeError: 'list' object is not an iterator可以看出zip()返回的就是迭代器对象,若是可迭代对象是没有next()方法的,就像上面的报错,换成迭代器就不会报错了 实例
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