Multiple statements in list compherensions in Python?

后端 未结 9 1413
不思量自难忘°
不思量自难忘° 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 08:03

    Here's an example from another question:

    [i for i,x in enumerate(testlist) if x == 1]
    

    the enumerate generator returns a 2-tuple which goes into i,x.

提交回复
热议问题