python - 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
我的大部分努力都可以来自另一篇文章。从另一个列表中删除出现在一个列表中的所有元素
感谢您的任何意见。
解决方案
for id in account_id:
id["competitor_new_unique"] = [x for x in id["competitor_new"] if x not in id["competitor_old"]]
推荐阅读
- python - 熊猫:列中的行
- python - 每次我运行代码时,Cnn + word2vec 模型都会不断改变准确性
- tensorflow - 如何在给定时间内睡觉和唤醒谷歌珊瑚开发板
- python-3.x - 如何在 KivyMD 中添加启动画面
- c++ - 有没有办法用 C 宏在两个列表上做笛卡尔积?
- javascript - 在反应中加载three.js纹理作为base64字符串
- android - 检查用户是否使用 Mapbox 偏离路线
- angular - formControlName 显示 [object,object]
- javascript - 在 JS + HTML 中按下时,如何制作相互禁用/启用的按钮?
- nix - 相同的代码和相同的编译器能否在不同的机器上生成不同的二进制文件?