繁体   English   中英

Python - 试图解压列表理解以理解它

[英]Python - trying to unpack a list comprehension to understand it

这是我试图弄清楚但似乎无法弄清楚的基本问题。 我有一个按预期工作的列表理解(对于 Advent of Code)。 为了我的参考,我正在尝试打开它。

raw_data = """
30373
25512
65332
33549
35390
"""

forest = [[int(x) for x in row] for row in raw_data.split()]
print(forest)

输出: [[3, 0, 3, 7, 3], [2, 5, 5, 1, 2], [6, 5, 3, 3, 2], [3, 3, 5, 4, 9], [3, 5, 3, 9, 0]]

所以我试图解压它以更好地理解它,但它并没有像我预期的那样工作。

t = []
for row in raw_data.split():
    for x in row:
        t.append(int(x))

print(t)

输出: [3, 0, 3, 7, 3, 2, 5, 5, 1, 2, 6, 5, 3, 3, 2, 3, 3, 5, 4, 9, 3, 5, 3, 9, 0]

我想这就是你要找的:

t = []
for row in raw_data.split():
    r = []
    for x in row:
        r.append(int(x))
    t.append(r)

打印(t)

暂无
暂无

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

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