if/else in a list comprehension

后端 未结 11 757
一个人的身影
一个人的身影 2020-11-21 11:44

How can I do the following in Python?

row = [unicode(x.strip()) for x in row if x is not None else \'\']

Essentially:

  1. replace
11条回答
  •  终归单人心
    2020-11-21 12:22

    There isn't any need for ternary if/then/else. In my opinion your question calls for this answer:

    row = [unicode((x or '').strip()) for x in row]
    

提交回复
热议问题