我有一个元组列表[('one','two','three'),('four','five','six'),('seven','eight','nine')]和一个清单['three','five','nine']我想要做的是删除与第二个清单,这样我就有了[('one','two'),('four','six'),('seven','eight')]有没有一种简单的方法可以做到这一点?
2 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
listoflists = [('one','two','three'),('four','five','six'),('seven','eight','nine')]
toremove = ['three','five','nine']
outcome = [tuple(item for item in list if item not in toremove) for list in listoflists]
幕布斯7119047
TA贡献1794条经验 获得超8个赞
创建一个新的元组列表
a=[('one','two','three'),('four','five','six'),('seven','eight','nine')]
b=['three','five','nine']
[set(i).difference(b) for i in a] # if you do not care about the order inside the tuple
添加回答
举报
0/150
提交
取消