【问题标题】:How to edit a string inside a list withPython如何使用 Python 编辑列表中的字符串
【发布时间】:2021-07-13 18:44:21
【问题描述】:

如果我有一个列表,每个项目都是一个字符串类型的项目。假设在每个项目中都有一组我要删除的特定单词。我该怎么做?

这是我自己的方法,但没有奏效。

        for row in new_basket:
            if "\n" in item:
                item = item.replace("\n", "")
            if "pattern" in row:
                item = item.replace("pattern", "")
            if "potato" in row:
                item = item.replace("potato", "")
            if "another text I don't want" in row:
                item = item.replace("another text I don't want", "")

示例输入:

['姓名:马克的土豆','出生日期:11/11/1111','详细信息:红色']

期望的输出: ['马克的土豆','11/11/1111','红色']

【问题讨论】:

  • 你能提供示例输入并期望输出吗?
  • 我可以举个例子是的。我会把它添加到问题中。
  • 你打电话给item 太早了。您应该搜索row。例如。 NameError: name 'item' is not defined
  • @DanielHao 完成。
  • @af3ld 我不明白。

标签: python string list


【解决方案1】:

对于这种情况使用regex,很简单!

import re

pattern = ["Name: Mark's potato", "Date of Birth: 11/11/1111", "Details: Red"]

for i in range(len(pattern)):
    pattern[i] = re.sub('.*:\s*|(\r\n|\r|\n)', '', pattern[i])

print(pattern)

【讨论】:

  • 这很好。这里唯一缺少的是删除新行。
  • @MYousefi 是的,我修好了。
  • 另外我会使用.*:\s* 而不是.*:. 作为前缀,只匹配空格而不删除任何实际字符。
  • @MYousefi 这是合理的!
【解决方案2】:

有人可能会有更好的方法来做到这一点,但以下内容应该可以帮助您:

mylist = ['test', 'cat', 'teeth', 'bite']

mylist = [s.replace('t','') for s in mylist if 't' in s]

mylist = [s.replace('c','') for s in mylist if 'c' in s]

【讨论】:

    【解决方案3】:

    我想我回答了你的问题希望我明白你的意思

    list_ = ["house", "potato", "pattern"]
    def list_replacer(original, replacement):
        if original in list_:
            i = list_.index(original)
            list_.pop(i)
            list_.insert(i, replacement)
            print(list_)
        
        else:
            print('The operation is impossible')
        
        
    list_replacer("house", "city")
    

    【讨论】:

      【解决方案4】:
      import re
      my_list = ["Name: Mark's potato", "Date of Birth: 11/11/1111", "Details: Red"]    
      keywords = ['\n', '\r', '\r\n'] # Common new line encoding
      # Put other keywords into the list
      # Note that if all of the strings look like with 'Keyword: Value' you can just append '^.*:\s*' in a lazy regex otherwise 
      keywords.append('Name: ')
      keywords.append('Date of Birth: ')
      keywords.append('Details: ')
      
      def replace(my_list, keywords):
          regex_pattern = '|'.join(keywords)
          return [re.sub(regex_pattern, '', s) for s in my_list]
      

      此功能一次只适用于一行。

      【讨论】:

        【解决方案5】:

        如果您的所有输入都与此示例相似,您可以通过以下方式获得所需的输出:

        your_list = ["Name: Mark's potato", "Date of Birth: 11/11/1111", "Details: Red"]
        
        your_desired_output = [li[1].strip() for li in [item.split(':') for item in your_list]]
        

        【讨论】:

          猜你喜欢
          • 2023-04-10
          • 2017-02-10
          • 2021-12-18
          • 1970-01-01
          • 2022-01-14
          • 1970-01-01
          • 2021-08-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多