【问题标题】:How to remove str. Of spaces from list [closed]如何删除str。列表中的空格[关闭]
【发布时间】:2021-10-04 15:52:43
【问题描述】:

A = ["a","b",","c",",","d"] 我怎样才能删除str。此列表中的空格????

【问题讨论】:

    标签: python string list helper spaces


    【解决方案1】:
    A = [s for s in A if s.strip()]
    

    上述代码有效,因为空字符串 - 即 '' - 评估为 False。

    【讨论】:

      【解决方案2】:

      A = [s for s in A if s.strip() != '']

      A = [s for s in A if len(s.strip()) > 0]

      【讨论】:

      • 因为空字符串 '' 评估为 false,您可以进一步简化; A = [s for s in A if s.strip()]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 2023-03-14
      • 1970-01-01
      • 2017-03-05
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多