环境
window10,pycharm2023.1.2
介绍
在Python中,如果你有一个列表,其中包含了字典,并且你想要去除重复的字典项,你可以使用几种不同的方法。以下是一些常见的方法:
方法 1: 使用集合和 frozenset
frozenset
是一个不可变的集合,可以作为字典的键。通过将字典转换为 frozenset
,我们可以利用集合的唯一性来去重。
def remove_duplicates_dicts(list_of_dicts):
seen = set()
result = []
for d in list_of_dicts:
# 将字典转换为 frozenset,以便可以用作集合的元素
t = frozenset(d.items())
if t not in seen:
seen.add(t)
result.append(d)
return result
# 示例
list_of_dicts = [
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 25},
{'name': 'Alice', 'age': 30},
{'name': 'Charlie', 'age': 35}
]
unique_dicts = remove_duplicates_dicts(list_of_dicts)
print(unique_dicts)
方法 2: 使用 json.dumps
将字典转换为 JSON 字符串,然后利用集合的唯一性来去重。
import json
def remove_duplicates_dicts(list_of_dicts):
seen = set()
result = []
for d in list_of_dicts:
# 将字典转换为 JSON 字符串
s = json.dumps(d, sort_keys=True)
if s not in seen:
seen.add(s)
result.append(d)
return result
# 示例
list_of_dicts = [
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 25},
{'name': 'Alice', 'age': 30},
{'name': 'Charlie', 'age': 35}
]
unique_dicts = remove_duplicates_dicts(list_of_dicts)
print(unique_dicts)
方法 3: 使用 pandas
库
如果你不介意引入第三方库,pandas
提供了一个非常简洁的方法来去重。
import pandas as pd
def remove_duplicates_dicts(list_of_dicts):
df = pd.DataFrame(list_of_dicts)
unique_df = df.drop_duplicates()
return unique_df.to_dict(orient='records')
# 示例
list_of_dicts = [
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 25},
{'name': 'Alice', 'age': 30},
{'name': 'Charlie', 'age': 35}
]
unique_dicts = remove_duplicates_dicts(list_of_dicts)
print(unique_dicts)
方法 4: 使用 itertools.groupby
如果你希望按某个特定键去重,可以使用 itertools.groupby
。
from itertools import groupby
from operator import itemgetter
def remove_duplicates_dicts(list_of_dicts, key):
# 按指定键排序
sorted_list = sorted(list_of_dicts, key=itemgetter(key))
# 使用 groupby 去重
unique_dicts = [next(group) for key, group in groupby(sorted_list, key=itemgetter(key))]
return unique_dicts
# 示例
list_of_dicts = [
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 25},
{'name': 'Alice', 'age': 30},
{'name': 'Charlie', 'age': 35}
]
unique_dicts = remove_duplicates_dicts(list_of_dicts, 'name')
print(unique_dicts)