Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何总结选择性字典值_Python 3.x - Fatal编程技术网

Python 3.x 如何总结选择性字典值

Python 3.x 如何总结选择性字典值,python-3.x,Python 3.x,我想过滤具体部门的薪资并进行总结,我已经做了上半年的工作,但是如果加上这些薪资,任何人都可以通过查看下面的代码提出建议 chennai = {"name": "Kumar", "Department": "Sales", "Age": 39,"Salary": 20} mumbai = {"name": "Suresh","Department": "Finance", "Age": 53,"Salary": 35} delhi = {"name": "Babu", "De

我想过滤具体部门的薪资并进行总结,我已经做了上半年的工作,但是如果加上这些薪资,任何人都可以通过查看下面的代码提出建议

chennai   = {"name": "Kumar", "Department": "Sales",     "Age": 39,"Salary": 20}
mumbai  = {"name": "Suresh","Department": "Finance",   "Age": 53,"Salary": 35}
delhi  = {"name": "Babu",  "Department": "QC",        "Age": 28,"Salary": 10}
kolkata = {"name": "Satish","Department": "Production","Age": 34,"Salary": 15}
madurai = {"name": "Dev",   "Department": "Management","Age": 45,"Salary": 23}
hyderabad = {"name": "Rani",  "Department": "Marketing", "Age": 46,"Salary": 25}
bengalore  = {"name": "Devi",  "Department": "Production","Age": 24,"Salary": 5}
cochin  = {"name": "Sarath","Department": "Production","Age": 26,"Salary": 12}
jaipur  = {"name": "Senu",  "Department": "Production","Age": 25,"Salary": 8}
shimla  = {"name": "Kumari","Department": "Management","Age": 37,"Salary": 20}
lucknow  = {"name": "Sanjay","Department": "Marketing", "Age": 52,"Salary": 30}

employ = [chennai,mumbai,delhi,kolkata,madurai,hyderabad,bengalore,cochin,jaipur,shimla,lucknow]

#Finding Production unit salary expenditure
for x in employ:
    sums = 0
    if x ["Department"] == 'Production':
        print x["Salary"]

我建议使用熊猫()

上面的代码看起来并不漂亮,但是熊猫非常强大。 以下是您如何按部门获得总工资的方法:

dataset3 = dataset.groupby('Department').sum()['Salary']
print (dataset3)
#Out[8]:
#Department
#Finance       35
#Management    43
#Marketing     55
#Production    40
#QC            10
#Sales         20
#Name: Salary, dtype: int64

如果你不想用熊猫试试这个

from collections import defaultdict
salary = defaultdict(int)
# for specific departments
required_departments = ["Production"]
for i in employ:
    if i["Department"] in required_departments:
        salary[i["Department"]] += i["Salary"]
print(salary)
# for all departments
salary = defaultdict(int)
for i in employ:
    salary[i["Department"]] += i["Salary"]

print(salary)

如果你想要一个班轮

result = sum(d.get("Salary", 0) for d in employ if d.get("Department") == "Production")
也可以对多个项目进行合计

departments = {"Production", "Marketing"}
result = sum(d.get("Salary", 0) for d in employ if d.get("Department") in departments)
departments = {"Production", "Marketing"}
result = sum(d.get("Salary", 0) for d in employ if d.get("Department") in departments)