简体   繁体   English

为什么列表列表 output 的 zip() 会这样?

[英]Why does zip() of list of lists output such?

Let's take:让我们来:

a = zip([[1,2],[2,3]])

where a is the zipped variable of list of lists [[1,2],[2,3]] the output for print(list(a)) is其中 a 是列表列表 [[1,2],[2,3]] 的压缩变量print(list(a))的 output 是

[([1, 2],), ([2, 3],)]

meaning the zip was a tuple containing ([1, 2],), ([2, 3],)?意味着 zip 是一个包含 ([1, 2],), ([2, 3],) 的元组? Why is that?这是为什么? For example, why is there a comma before both end parentheses of the tuple?例如,为什么元组的两个结束括号前都有一个逗号?

If someone could walk me through the computational steps I would greatly appreciate it.如果有人可以引导我完成计算步骤,我将不胜感激。

"The zip() function takes iterables (can be zero or more), aggregates them in a tuple, and return it." “zip() function 接受迭代(可以是零个或多个),将它们聚合在一个元组中,然后返回它。”

Basically it mean that you can do sometime like that:基本上,这意味着您有时可以这样做:

list1 = [1,2,3,4]
list2 = ["h", "b", "s"]

for num, char in zip(list1, list2):
    print(num, char)

# output:
# 1 h
# 2 b
# 3 s

enter value: [[1,2],[2,3]]输入值: [[1,2],[2,3]]

first you zip the list: ([1, 2],), ([2, 3],) or just make tuples to the values首先你 zip 列表: ([1, 2],), ([2, 3],)或者只是对值进行元组

if you just enter a one list:如果你只输入一个列表:

a = zip([1,2,3,4,5,6])

print(list(a))  # [(1,), (2,), (3,), (4,), (5,), (6,)]

it make the values in the list a tuples.它使列表中的值成为元组。 (, for make it a one value tuple) (, 使它成为一个单值元组)

and after that you make it a list: [([1, 2],), ([2, 3],)]然后你把它做成一个列表: [([1, 2],), ([2, 3],)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM