首页 > 解决方案 > Python:为每一行从另一个列表中删除出现在一个列表中的字符串

问题描述

假设我每个帐户 ID 有两个列表(下表)。我想删除列表 1 (competitor_old) 中出现的任何元素,这些元素也出现在列表 2 (competitor_new) 中。L2 - L1每行必备。

因此,从下表中,我将有一个新列(competitor_new_unique),第一行的 a 值为 c,d

桌子

      account_id competitor_old competitor_new 
          234241            a,b        a,b,c,d
           53266              h            h,s
          342536              j              j
          325632              s          s,g,e
          324346              f              f

期望的结果

 account_id competitor_old competitor_new competitor_new_unique
      234241            a,b        a,b,c,d                   c,d
       53266              h            h,s                     s
      342536              j              j
      325632              s          s,g,e                   g,e
      324346              f              f

我的大部分努力都可以来自另一篇文章。从另一个列表中删除出现在一个列表中的所有元素

感谢您的任何意见。

标签: pythonlist

解决方案


for id in account_id:
    id["competitor_new_unique"] = [x for x in id["competitor_new"] if x not in id["competitor_old"]] 

推荐阅读