繁体   English   中英

Python:为每一行从另一个列表中删除出现在一个列表中的字符串

[英]Python: Remove string that occurs in one list from another for every row

假设我每个帐户 ID 有两个列表(下表)。 我想删除出现在列表 1 (competitor_old) 中但也出现在列表 2 (competitor_new) 中的任何元素。 基本L2 - L1每行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

我的大部分努力都可以从另一个帖子中获得。 从另一个列表中删除出现在一个列表中的所有元素

感谢您提供任何意见。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM