根据元组中的字典值删除元组列表中的元组

我有一长串包含字典的元组。我想删除“权重”值为 1 的元组。


List_example = [('WHT', 'WML, {'weight': 48}),

 ('WHHT','CCH', {'weight': 53}),

 ('WRT','KUF', {'weight': 1}),

 ('WHHT','RWH',{'weight': 1}))]

期望的输出:


List_example = [('WHT', 'WML, {'weight': 48}),

 ('WHHT','CCH', {'weight': 53}))]


眼眸繁星
浏览 98回答 1
1回答

慕姐4208626

以下lst = [('WHT', 'WML', {'weight': 48}), ('WHHT','CCH', {'weight': 53}), ('WRT','KUF', {'weight': 1}), ('WHHT','RWH',{'weight': 1})] new_lst = [x for x in lst if x[2]['weight'] != 1]print(new_lst)输出[('WHT', 'WML', {'weight': 48}), ('WHHT', 'CCH', {'weight': 53})]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python