list dict group by

GisangLee·2022년 4월 11일
0

my_module

목록 보기
13/33
post-custom-banner
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))
profile
포폴 및 이력서 : https://gisanglee.github.io/web-porfolio/
post-custom-banner

0개의 댓글