我可以在列表理解中初始化变量吗?

问题描述 投票:0回答:1
for col in ws:
    for value in col:
        new_value = value * 2
        blank_list.append(new_value)

对此的列表理解应该是什么? 我是这样想的,但我不知道之后该写什么-

blank_list = [new_value for col in ws for value in col]

这之后我不知道该怎么办?

python list-comprehension
1个回答
0
投票
blank_list = [value*2 for col in ws for value in col] 

这会给你你期望的结果。

例子:

ws = [[1, 2, 3], [4, 5, 6]]
blank_list = [value*2 for col in ws for value in col] 
print(blank_list)

# [[2, 4, 6], [8, 10, 12]]
© www.soinside.com 2019 - 2024. All rights reserved.