Temporary variable within list comprehension

后端 未结 3 2136
[愿得一人]
[愿得一人] 2021-02-19 05:07

It happens to me quite often to have a piece of code that looks like this.

raw_data  = [(s.split(\',\')[0], s.split(\',\')[1]) for s in all_lines if s.split(\',         


        
3条回答
  •  一向
    一向 (楼主)
    2021-02-19 05:22

    You can't.

    A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

    From here

    Assignment in Python is not an expression.

    As Padraic Cunningham comments - if you need to split it multiple times don't do it in list comprehension.

提交回复
热议问题