[英]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.