我有一长串包含字典的元组。我想删除“权重”值为 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}))]
1 回答
慕姐4208626
TA贡献1852条经验 获得超7个赞
以下
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})]
添加回答
举报
0/150
提交
取消