Python中按特定键对字典数据进行分组列表
Python中按特定键对字典数据进行分组列表可以使用 itertools.groupby() 方法完成。
Itertools.groupby()
此方法计算 iterable 中存在的每个元素的键。它返回分组项的键和可迭代项。
Syntax: itertools.groupby(iterable, key_func)
Parameters:
- iterable: Iterable can be of any kind (list, tuple, dictionary).
- key_func: A function that calculates keys for each element present in iterable.
Return type: It returns consecutive keys and groups from the iterable. If the key function is not specified or is None, key defaults to an identity function and returns the element unchanged.
让我们看看例子:
示例 1:假设我们有员工和公司字典列表。
INFO = [
{'employee': 'XYZ_1', 'company': 'ABC_1'},
{'employee': 'XYZ_2', 'company': 'ABC_2'},
{'employee': 'XYZ_3', 'company': 'ABC_3'},
{'employee': 'XYZ_4', 'company': 'ABC_3'},
{'employee': 'XYZ_5', 'company': 'ABC_2'},
{'employee': 'XYZ_6', 'company': 'ABC_3'},
{'employee': 'XYZ_7', 'company': 'ABC_1'},
{'employee': 'XYZ_8', 'company': 'ABC_2'},
{'employee': 'XYZ_9', 'company': 'ABC_1'}
]
现在我们需要通过“公司”键名显示所有数据组。
代码:
Python3
# import a groupby() method
# from itertools module
from itertools import groupby
# dictionary
INFO = [
{'employee': 'XYZ_1', 'company': 'ABC_1'},
{'employee': 'XYZ_2', 'company': 'ABC_2'},
{'employee': 'XYZ_3', 'company': 'ABC_3'},
{'employee': 'XYZ_4', 'company': 'ABC_3'},
{'employee': 'XYZ_5', 'company': 'ABC_2'},
{'employee': 'XYZ_6', 'company': 'ABC_3'},
{'employee': 'XYZ_7', 'company': 'ABC_1'},
{'employee': 'XYZ_8', 'company': 'ABC_2'},
{'employee': 'XYZ_9', 'company': 'ABC_1'}
]
# define a fuction for key
def key_func(k):
return k['company']
# sort INFO data by 'company' key.
INFO = sorted(INFO, key=key_func)
for key, value in groupby(INFO, key_func):
print(key)
print(list(value))
Python3
# import required methods
from itertools import groupby
from operator import itemgetter
# dictionary
students = [
{'mark': '65', 'grade': 'C'},
{'mark': '86', 'grade': 'A'},
{'mark': '73', 'grade': 'B'},
{'mark': '49', 'grade': 'D'},
{'mark': '91', 'grade': 'A'},
{'mark': '79', 'grade': 'B'}
]
# Sort students data by grade key.
students = sorted(students,
key = itemgetter('grade'))
# Display data grouped by grade
for key, value in groupby(students,
key = itemgetter('grade')):
print(key)
for k in value:
print(k)
输出:
ABC_1
[{’employee’: ‘XYZ_1’, ‘company’: ‘ABC_1′}, {’employee’: ‘XYZ_7’, ‘company’: ‘ABC_1′}, {’employee’: ‘XYZ_9’, ‘company’: ‘ABC_1′}]
ABC_2
[{’employee’: ‘XYZ_2’, ‘company’: ‘ABC_2′}, {’employee’: ‘XYZ_5’, ‘company’: ‘ABC_2′}, {’employee’: ‘XYZ_8’, ‘company’: ‘ABC_2′}]
ABC_3
[{’employee’: ‘XYZ_3’, ‘company’: ‘ABC_3′}, {’employee’: ‘XYZ_4’, ‘company’: ‘ABC_3′}, {’employee’: ‘XYZ_6’, ‘company’: ‘ABC_3’}]
示例 2:假设我们有学生成绩和分数的字典列表。
students = [
{'mark': '65','grade': 'C'},
{'mark': '86','grade': 'A'},
{'mark': '73','grade': 'B'},
{'mark': '49','grade': 'D'},
{'mark': '91','grade': 'A'},
{'mark': '79','grade': 'B'}
]
现在我们需要通过“等级”键显示所有数据组。
代码:
Python3
# import required methods
from itertools import groupby
from operator import itemgetter
# dictionary
students = [
{'mark': '65', 'grade': 'C'},
{'mark': '86', 'grade': 'A'},
{'mark': '73', 'grade': 'B'},
{'mark': '49', 'grade': 'D'},
{'mark': '91', 'grade': 'A'},
{'mark': '79', 'grade': 'B'}
]
# Sort students data by grade key.
students = sorted(students,
key = itemgetter('grade'))
# Display data grouped by grade
for key, value in groupby(students,
key = itemgetter('grade')):
print(key)
for k in value:
print(k)
输出:
A
{'mark': '86', 'grade': 'A'}
{'mark': '91', 'grade': 'A'}
B
{'mark': '73', 'grade': 'B'}
{'mark': '79', 'grade': 'B'}
C
{'mark': '65', 'grade': 'C'}
D
{'mark': '49', 'grade': 'D'}