我有一個元組列表[('one','two','three'),('four','five','six'),('seven','eight','nine')]和一個清單['three','five','nine']我想要做的是刪除與第二個清單,這樣我就有了[('one','two'),('four','six'),('seven','eight')]有沒有一種簡單的方法可以做到這一點?
2 回答

縹緲止盈
TA貢獻2041條經(jīng)驗 獲得超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條經(jīng)驗 獲得超8個贊
創(chuàng)建一個新的元組列表
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
提交
取消