replace empty string(s) in tuple

前端 未结 1 1017
终归单人心
终归单人心 2020-12-18 08:17

Is there an easy way (hopefully a one liner) to replace \'\' with something like \'-\'?
Many thanks.

tup = (1,2,\'ABC\',\'\',\'\',\'\',\'text\')
<         


        
相关标签:
1条回答
  • 2020-12-18 08:30

    How about the following?

     tuple('-' if x == '' else x for x in tup)
    

    As Felix Kling comments, tuples are immutable, so the best you can do is to return a new one.

    0 讨论(0)
提交回复
热议问题