我有一个字典列表:
records = [{id:123, name:'Course A', enrolled_date:'1st Feb'},
{id:123, name:'Course A', enrolled_date:'1st Jan'},
{id:456, name:'Course B', enrolled_date:'1st Jan'}]
我想根据这些数据创建一个新的字典列表。新列表应仅包含ids在该列表范围内具有唯一性的字典,例如:
new_records = [{id:123, name:'Course A'},
{id:456, name:'Course B'}]
这是我到目前为止尝试过的代码。它显然不起作用 - 但我已经竭尽全力纠正它。
new_records = []
for record in records:
new_id = record['id']
new_name = record['name']
new_dict = {'id':new_id, 'name',new_name}
If new_id not in new_records:
new_records.append(new_dict)
翻阅古今
莫回无
相关分类