0

I have a list of campuses:

campus = [{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'},{'id': '3', 'dlin': '1'},{'id': '4', 'dlin': '2'},{'id': '5', 'dlin': '2'},{'id': '6', 'dlin': '1'}, ]

each campus belongs to a school with a unique dlin. I want to have a list in which I have some other lists, each having a few dictionaries. I run the below code:

schools = []
for i in campus:
    ls = []
    for j in campus:
        if i['dlin'] == j['dlin']:
            ls.append(j)
            # campus_copy.remove(j)
    schools.append(ls)
[print(item) for item in schools]

the result is:

[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '4', 'dlin': '2'}, {'id': '5', 'dlin': '2'}]
[{'id': '4', 'dlin': '2'}, {'id': '5', 'dlin': '2'}]
[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]

I have to either remove the duplicate members from schools or modify the code such that I do not get duplicates. When I try to remove duplicates from schools, I see that dic item is not hashable so I can not do it. To solutions are available that are somewhat similar to my problem. Remove duplicates from list of dictionaries within list of dictionaries Remove duplicate dict in list in Python However, I cannot figure out what to do? does anybody know how to solve the problem?

what I expect to get is:

[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '4', 'dlin': '2'}, {'id': '5', 'dlin': '2'}]
4

2 回答 2

3

一种可能的解决方案是将dlinas 键存储在字典中(并且字典不能有多个相等的键),而不是之后显式删除重复项:

campus = [{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'},{'id': '3', 'dlin': '1'},{'id': '4', 'dlin': '2'},{'id': '5', 'dlin': '2'},{'id': '6', 'dlin': '1'}, ]

schools = {}
for c in campus:
    schools.setdefault(c['dlin'], []).append(c)

for s in schools.values():
    print(s)

印刷:

[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '4', 'dlin': '2'}, {'id': '5', 'dlin': '2'}]
于 2019-08-20T13:49:41.520 回答
0

根据 Andrej 的回答,我解决了我遇到的问题的另一部分,我只想在这里分享:

我的问题:

我现在涉及与前一个问题相关的另一个问题:

我有这个字典列表,一个校园的每个信息。多个校区可能属于一所学校。我必须根据它们名称的相似性来区分和聚类它们。

campus = [
{'id': '1', 'name': 'seneca - york'}, 
{'id': '2', 'name': 'seneca college - north gate campus'},
{'id': '3', 'name': 'humber college - toronto campus'},
{'id': '4', 'name': 'humber college'},
{'id': '5', 'name': 'humber collge - waterloo campus'},
{'id': '6', 'name': 'university of waterloo toronto campus'}, 
]

这个小而整洁的代码可以达到我的预期结果:

schools = {}
for c in campus:
    schools.setdefault(c['name'][:4], []).append(c)
print(schools)
于 2019-08-20T15:05:42.440 回答