Multiple statements in list compherensions in Python?

后端 未结 9 1442
不思量自难忘°
不思量自难忘° 2021-02-13 07:09

Is it possible to have something like:

list1 = ...

currentValue = 0
list2 = [currentValue += i, i for i in list1]

I tried that but didn\'t wor

9条回答
  •  借酒劲吻你
    2021-02-13 07:48

    Why would you create a duplicate list. It seems like all that list comprehension would do is just sum the contents.

    Why not just.

    list2 = list(list1)   #this makes a copy
    currentValue = sum(list2)
    

提交回复
热议问题