【问题标题】:Python: Replace "-" with whitespacePython:用空格替换“-”
【发布时间】:2016-07-04 16:30:25
【问题描述】:

我一直坚持在 Python 中用空格替换“-”。我搜索了 Stack Overflow 并尝试了下面的代码,但它并没有达到我想要的效果。

import string

text1 = ['why-wont-these-dashes-go-away']
for i in text1:
 str(i).replace("-", " ")
print "output 1: " 
print text1

text2 = ['why-wont-these-dashes-go-away']
text2 = [x.strip('-') for x in text2]
print "output 2: " 
print text2

text3 = ['why-wont-these-dashes-go-away']
text3 = [''.join(c for c in s if c not in string.punctuation) for s in text3]
print "output 3: " 
print text3

text4 = ['why-wont-these-dashes-go-away']
text4 = [' '.join(c for c in s if c not in string.punctuation) for s in text3]
print "output 4: " 
print text4

这是我的输出:

output 1: 
['why-wont-these-dashes-go-away']
output 2: 
['why-wont-these-dashes-go-away']
output 3: 
['whywontthesedashesgoaway']
output 4: 
['w h y w o n t t h e s e d a s h e s g o a w a y']

这就是我想要的:

['why wont there dashes go away']

我知道 text1、text2 和 text3 是每个列表,其中一个项目是一个字符串。这可能是我忽略的一些小东西,有什么想法吗?

【问题讨论】:

  • 如果您将replace 的返回值重新分配给列表,则输出1 将起作用...strip 仅用于结束字符...另外2 个逐字符循环
  • 你做错了几件事。请参阅official Python tutorial

标签: python string list replace


【解决方案1】:

text1 是一个列表,该列表有一个元素,它是第 0 个位置的字符串“为什么这些破折号不会消失”。所以,只需使用:

text1 = [text1[0].replace('-',' ')]

print text1
['why wont these dashes go away']

【讨论】:

    【解决方案2】:

    您有以下错误:

    方法一:你没有replace的返回值赋给任何变量

    方法2:Strip只剥离字符串开头和结尾的字符

    方法 3 和 4:您使用空字符串 ('') 或空格 (' ') 连接每个字符,不是每个单词。

    你可以试试这个方法:

    text1 = [x.replace('-', ' ') for x in text1]
    

    或者这个:

    text1 = [' '.join(x.split('-')) for x in text1]
    

    【讨论】:

      【解决方案3】:

      您的第一个示例不起作用,因为将 i 转换为 str 会生成字符串的副本,然后您将其修复。做吧:

      i.replace("-", " ")

      您的第二个示例使用 strip,这不是您想要的。

      您的第三个和第四个示例消除了破折号,这也不起作用。

      【讨论】:

        【解决方案4】:

        您在循环中所做的操作对列表中的数据没有影响,您应该做的是使用您的数据创建一个新列表:

        [s.replace('-', ' ') for s in text1]
        

        【讨论】:

          【解决方案5】:

          你的 ['why-wont-these-dashes-go-away'] 已经是一个列表元素。所以你只需要做

          text1 = ['why-wont-these-dashes-go-away']
          print text1[0]..replace('-',' ')
          

          【讨论】:

            【解决方案6】:

            你可以分开-join

            在输出 1 之后,您还需要重新分配列表的值

            for i,s in enumerate(text1):
                text1[i] = ' '.join(s.split('-')) 
            

            【讨论】:

              【解决方案7】:
              list1 = ['why-wont-these-dashes-go-away', 'a-sentence-with-dashes']
              list1 = [text.replace('-',' ') for text in list1]
              print list1
              
              Output: ['why wont these dashes go away', 'a sentence with dashes']
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2021-10-29
                • 2021-05-11
                • 1970-01-01
                • 2013-12-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多