如何使用列表推导式使嵌套列表中的第一项为 0?

问题描述 投票:0回答:1

例如,如果我有一个类似的列表

num_list = [[1234, 1244123, 453242], [123123, 78421, 76433], [46345, 578542, 47463]]
我需要它返回
[[0, 1244123, 453242], [0, 78421, 76433], [0, 578542, 47463]]
我该如何通过列表理解来做到这一点?

我知道如何使用嵌套 for 循环来做到这一点。 我尝试做一些类似的事情

[[x * 0 for x in j[0]] for j in num_list]

python list-comprehension nested-lists
1个回答
1
投票
num_list = [[0] + i[1:] for i in num_list]

输出:

[[0, 1244123, 453242], [0, 78421, 76433], [0, 578542, 47463]]
© www.soinside.com 2019 - 2024. All rights reserved.