List comprehensions splitting loop variable

后端 未结 2 646
北海茫月
北海茫月 2021-01-07 18:37

I am trying to find out if there is a way to split the value of each iteration of a list comprehension only once but use it twice in the output.

As an example of th

2条回答
  •  没有蜡笔的小新
    2021-01-07 19:19

    You could use a list comprehension wrapped around a generator expression:

    [(x[1],x[2]) for x in (x.split(";") for x in a.split("\n")) if x[1] != 5]
    

提交回复
热议问题