应按特定值过滤的字典列表

Orh*_*nC1 -1 python dictionary list

我有一个dicts列表,并希望只有基于某些键的唯一值.

可以说我有

data = [{name: "John", age: "20", occupation: "Python pro"}, \
        {name: "Jack", age: "80", occupation: "Olympic Athlete"}, \
        {name: "John", age: "20", occupation: "Student"}]
Run Code Online (Sandbox Code Playgroud)

我想根据键的名称和'年龄'来减少它看起来像

[{name: "John", age: "20", occupation: "Python pro"}, \
 {name: "Jack", age: "80", occupation: "Olympic Athlete"}]
Run Code Online (Sandbox Code Playgroud)

我已经尝试了以下但没有意识到明显的问题,它基本上每次都会添加所有内容.

    all_drives =  drivesInBuilder("object") + drivesInBuilder("account") + drivesInBuilder("container")
    reduced_list = [(x["ip address"], x["name"]) for x in all_drives]
    unique_list = list(set(reduced_list))
    unique_drives = [x for x in all_drives if (x["ip address"], x["name"]) in unique_list]
    print(unique_drives)
Run Code Online (Sandbox Code Playgroud)

mVC*_*Chr 5

data = [{"name": "John", "age": "20", "occupation": "Python pro"}, \
        {"name": "Jack", "age": "80", "occupation": "Olympic Athlete"}, \
        {"name": "John", "age": "20", "occupation": "Student"}]

new_data = []
names_ages = set([])
for d in data:
    name_age = (d["name"], d["age"])
    if name_age not in names_ages:
        new_data.append(d)
    names_ages.add(name_age)

print new_data
# [{'age': '20', 'name': 'John', 'occupation': 'Python pro'},
#  {'age': '80', 'name': 'Jack', 'occupation': 'Olympic Athlete'}]
Run Code Online (Sandbox Code Playgroud)