【问题标题】:Remove specific words by comparing two lists [duplicate]通过比较两个列表来删除特定单词[重复]
【发布时间】:2019-10-31 16:11:16
【问题描述】:

我有两个列表。

x=['billed_qty','billed_amt','sale_value']

y=['george billed_qty', 'sam billed_amt', 'ricky sale_value', 'donald billed_qty']

我需要消除列表 y 中出现在列表 x 中的单词,并希望得到的列表为:

z=['george','sam','ricky','donald']

我怎样才能做到这一点?

谢谢

【问题讨论】:

  • 我讨厌成为“那个人”,但您可能想考虑降低数据结构的复杂性。
  • @LogicalBranch 做那个给出明智建议而不是急于回答问题的人没什么错。

标签: python list


【解决方案1】:

在列表理解中使用 str.joinstr.split

z = [' '.join(w for w in s.split() if w not in x) for s in y]
print(z)

输出:

['george', 'sam', 'ricky', 'donald']

【讨论】:

    【解决方案2】:

    regexlist comprehension 一起使用:

    comp = re.compile('|'.join(x))
    z = [re.sub(comp, '', i).strip() for i in y]
    
    print(z)
    ['george','sam','ricky','donald']
    

    【讨论】:

      【解决方案3】:

      为什么不:

      print([' '.join(set(i.split()).difference(set(x))) for i in y])
      

      输出:

      ['george', 'sam', 'ricky', 'donald']
      

      【讨论】:

        【解决方案4】:

        首先,split y 的元素:

        for i in range(0,len(y)):
            y[i] = y[i].split(' ')
        

        所以,y 是:

        [['george', 'billed_qty'], ['sam', 'billed_amt'], ['ricky', 'sale_value'], ['donald', 'billed_qty']]
        

        现在,检查x 的元素是否存在于y

        for i in range(0,len(y)):
            for j in range(0,len(x)):
                if x[j] in y[i][1]:
                    y[i] = y[i][0] 
        

        y 变为:

        ['george', 'sam', 'ricky', 'donald']
        

        【讨论】:

          【解决方案5】:

          为此,您可以使用itertools 解决它。

          解决方法如下..

          import itertools
          
          z = [i.split() for i in y]
          
          # This gives us z = [['george', 'billed_qty'], ['sam', 'billed_amt'], ['ricky', 'sale_value'], ['donald', 'billed_qty']]
          
          w = list(itertools.chain.from_iterable(z))
          
          # This gives us w = ['george', 'billed_qty', 'sam', 'billed_amt', 'ricky', 'sale_value', 'donald', 'billed_qty']
          
          output = [a for a in w if a not in x]
          
          # This gives us output = ['george', 'sam', 'ricky', 'donald']
          
          

          【讨论】:

            【解决方案6】:

            我不知道它是否涵盖了您的所有情况,但一个简单的解决方案是:

            for i in x:
              for idx, k in enumerate(y):
                y[idx] = k.replace(" "+i, "")
            

            对于array x 中的每个值,将array y 中的值替换为空字符串(包括左侧的空格)。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2022-01-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-06
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多