【问题标题】:How to replace a word to another word in a list python?如何将一个单词替换为列表python中的另一个单词?
【发布时间】:2017-08-19 17:37:40
【问题描述】:

如果我有这个列表:

['hello', 'world', 'goodbye', 'world', 'food']

是否可以在不使用任何自动命令的情况下将每个 world 替换为任何其他单词,我正在考虑这样的事情:

if 'world' in list:
    'world' in list == 'peace'

【问题讨论】:

  • 什么是自动命令?
  • @MadPhysicist 类似 list.replace()

标签: python python-2.7 list


【解决方案1】:

您可以使用带有 if-else 的列表推导式。

list_A = ['hello', 'world', 'goodbye', 'world']
list_B = [word if word != 'world' else 'friend' for word in list_A]

您现在有了一个新列表list_B,其中“世界”一词的所有实例都已替换为“朋友”。

【讨论】:

    【解决方案2】:

    如果您的列表中有唯一值:

    my_list[my_list.index('old_word')]='new_word' 
    

    【讨论】:

      【解决方案3】:

      我认为没有现成的解决方案,例如您正在寻找的类似 my_list.replace() 的东西。因此,简单的解决方案就是遍历整个列表(使用 enumerate 来保留迭代变量)。

      试试这个:

      for index, elem in enumerate(my_list):
          if elem == "world":
              my_list[index] = "peace"
      

      【讨论】:

        【解决方案4】:

        我不知道我是否理解你的问题,但它应该有效:

        lst = ['hello', 'world', 'goodbye', 'world', 'food']
        
        lst = [i.replace('world', 'peace') for i in lst]
        
        print(lst2)
        

        "If" 语句是不需要的。因为如果 'world' 不存在,它会忽略它

        【讨论】:

        • 这将失败,并带有“黑社会”之类的词。
        【解决方案5】:

        虽然python标准库中没有list.replace()方法。

        但是这个实用功能可以帮助你:

        def replace(l, target_word, replaced_word):
            for index, element in enumerate(l):
                if element == target_word:
                    l[index] = replaced_word
        
            return l
        

        【讨论】:

          【解决方案6】:

          这是对 Zach 回答中发生的事情的扩展视图

          word_list = ['hello', 'world', 'goodbye', 'world', 'food']
          for ndx, word in enumerate(word_list):
              if word == 'world':
                  word_list[ndx] = 'peace'
          print word_list
          # result: ['hello', 'peace', 'goodbye', 'peace', 'food']
          

          【讨论】:

          • 我认为if 语句应该是if word == 'world' 而不是if 'world' in word_list
          • 你知道你应该枚举word_list
          • 修复了这个问题
          【解决方案7】:
          def customReplaceFunction(wordList, replacingWord):
              for i in range(len(wordList)):
                  if wordList[i] == 'world':
                      wordList[i] = replacingWord
              print wordList
          

          【讨论】:

            【解决方案8】:

            遍历列表并将“世界”替换为“和平”

            wordlist = ['hello', 'world', 'goodbye', 'world', 'food']
            
            
            for i in range(0,len(wordlist)):
               if wordlist[i]=="world":
                  wordlist[i]="peace"
            print(wordlist) 
            
            wordlist = ['hello', 'peace', 'goodbye', 'peace', 'food']  
            

            【讨论】:

              【解决方案9】:

              您可以映射每个世界并用合适的词替换。

              使用 lambda 函数,

              words = map(lambda word: word.replace('world', 'peace') , l)
              

              【讨论】:

                猜你喜欢
                • 2020-09-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-03-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多