简体   繁体   English

如何在列表理解中赋值?

[英]How to assign a value in list comprehension?

list4 = [[10, 20, 40], [40, 50, 60], [70, 80, 90]]
for i in range(len(list4)):
    list4[i][-1] = 100
print(list4)

It works, but how could i set a value in list comprehension它有效,但我怎么能在列表理解中设置一个值

list5 = [i for i in range(len(list4)) list4[i][-1] = 100]

I think the following list comprehension gives same operation as the for loop:我认为以下列表推导给出了与 for 循环相同的操作:

list5 = [arr[:-1]+[100] for arr in list4]

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

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