问候所有人,我的python文件中有一个列表,根据此列表,我如何计算每个员工A,B和C的actual_duration
的总和?
以下是我的清单:
[{'employee_name': 'A', 'actual_duration': 14},
{'employee_name': 'A', 'actual_duration': 6},
{'employee_name': 'B', 'actual_duration': 8},
{'employee_name': 'B', 'actual_duration': 22},
{'employee_name': 'C', 'actual_duration': 15}]
答案 0 :(得分:1)
一种方法是使用collections.defaultdict
l = [ {'employee_name': 'A', 'actual_duration': 14},
{'employee_name': 'A', 'actual_duration': 6},
{'employee_name': 'B', 'actual_duration': 8},
{'employee_name': 'B', 'actual_duration': 22},
{'employee_name': 'C', 'actual_duration': 15} ]
from collections import defaultdict
d_dict = defaultdict(int)
for k,v in [d.values() for d in l]:
d_dict[k]+=v
new_l = []
for k,v in d_dict.items():
new_l.append({'employee_name': k, 'actual_duration': v})
new_l
输出:
[{'actual_duration': 20, 'employee_name': 'A'},
{'actual_duration': 30, 'employee_name': 'B'},
{'actual_duration': 15, 'employee_name': 'C'}]
答案 1 :(得分:0)
尝试以下操作,使用for
循环迭代原始列表并连接项目:
for entry in times:
if entry['employee_name'] not in map(lambda i: i['employee_name'], aggregate):
aggregate.append(entry)
else:
aggregate[map(lambda i: i['employee_name'], aggregate).index(entry['employee_name'])]['actual_duration']+=entry['actual_duration']
>>> times = [{'employee_name': 'A', 'actual_duration': 14},
... {'employee_name': 'A', 'actual_duration': 6},
... {'employee_name': 'B', 'actual_duration': 8},
... {'employee_name': 'B', 'actual_duration': 22},
... {'employee_name': 'C', 'actual_duration': 15}]
>>> aggregate = []
>>> for entry in times:
... if entry['employee_name'] not in map(lambda i: i['employee_name'], aggregate):
... aggregate.append(entry)
... else:
... print map(lambda i: i['employee_name'], aggregate).index(entry['employee_name'])
... aggregate[map(lambda i: i['employee_name'], aggregate).index(entry['employee_name'])]['actual_duration']+=entry['actual_duration']
...
0
1
>>> aggregate
[{'actual_duration': 20, 'employee_name': 'A'}, {'actual_duration': 30, 'employee_name': 'B'}, {'actual_duration': 15, 'employee_name': 'C'}]
>>>
答案 2 :(得分:0)
有两种方法,一种是使用默认字典:
your_data=[{'employee_name': 'A', 'actual_duration': 14},
{'employee_name': 'A', 'actual_duration': 6},
{'employee_name': 'B', 'actual_duration': 8},
{'employee_name': 'B', 'actual_duration': 22},
{'employee_name': 'C', 'actual_duration': 15}]
from collections import defaultdict
default_dict=defaultdict(list)
for i in your_data:
track=[]
for j,l in i.items():
[track.insert(0, (l, j)) if isinstance(l,int) else track.insert(1,(l,j))]
default_dict[track[1]].append(track[0][0])
print({'employee_name : {}'.format(key[0]):'actual_duration : {} '.format(sum(values)) for key,values in default_dict.items()})
输出:
{'employee_name : A': 'actual_duration : 20 ', 'employee_name : C': 'actual_duration : 15 ', 'employee_name : B': 'actual_duration : 30 '}
不使用default_dict的第二个详细解决方案:
new={}
for i in your_data:
track=[]
for j,l in i.items():
[track.insert(0, (l, j)) if isinstance(l,int) else track.insert(1,(l,j))]
if track[1] not in new:
new[track[1]]=[track[0][0]]
else:
new[track[1]].append(track[0][0])
print({'employee_name : {}'.format(key[0]):'actual_duration : {} '.format(sum(values)) for key,values in new.items()})
答案 3 :(得分:0)
只需使用pandas
import pandas as pd
data = [{'employee_name': 'A', 'actual_duration': 14},
{'employee_name': 'A', 'actual_duration': 6},
{'employee_name': 'B', 'actual_duration': 8},
{'employee_name': 'B', 'actual_duration': 22},
{'employee_name': 'C', 'actual_duration': 15}]
result = pd.DataFrame(data).groupby('employee_name').sum()
print(result)
actual_duration
employee_name
A 20
B 30
C 15
如果你想要一个字典,你可以做
result_dict = result.reset_index().to_dict('records')
print(result_dict)
[{'actual_duration': 20, 'employee_name': 'A'},
{'actual_duration': 30, 'employee_name': 'B'},
{'actual_duration': 15, 'employee_name': 'C'}]