python 中员工字典数据库中的计数值
Counting values in an employee dictionary database in python
python 对我来说是新语言,所以这个问题听起来很简单,但如果有人能指出正确的方向,我将不胜感激!我创建了一个名为 employees 的字典,它对他们有一些价值:
我想知道每个部门有多少人,例如:tech-2,accounting-1。
我有这样的东西,但它打印出来是空白的。
def main():
employees= {'name': 'John', 'empID': '102', 'dpt': 'tech', 'title':
'programmer', 'salary': '75'}
{'name': 'Jane', 'empID': '202', 'dpt': 'tech', 'title': 'programmer',
'salary': '80'}
{'name': 'Joe', 'empID': '303', 'dpt': 'accounting', 'title':
'accountant', 'salary': '85'}
for item in employees:
dic = employees[item]
if dic['dpt'[0]]==dic['dpt'[1]]:
duplicate += 1
print("there are "+ duplicate)
else:
print("There are no duplicate")
如果您不想导入 Counter
,请使用字典来跟踪每个部门的员工人数:
employee_list = [{
'name': 'John',
'empID': '102',
'dpt': 'tech',
'title': 'programmer',
'salary': '75'
}, {
'name': 'Jane',
'empID': '202',
'dpt': 'tech',
'title': 'programmer',
'salary': '80'
}, {
'name': 'Joe',
'empID': '303',
'dpt': 'accounting',
'title': 'accountant',
'salary': '85'
}]
department_to_count = dict()
for employee in employee_list:
department = employee["dpt"]
if department not in department_to_count:
department_to_count[department] = 0
department_to_count[department] += 1
for department, employee_count in department_to_count.items():
print("Department {} has {} employees".format(department,
employee_count))
from collections import Counter
employees = [{'name': 'John', 'empID': '102', 'dpt': 'tech', 'title': 'programmer', 'salary': '75'},
{'name': 'Jane', 'empID': '202', 'dpt': 'tech', 'title': 'programmer', 'salary': '80'},
{'name': 'Joe', 'empID': '303', 'dpt': 'accounting', 'title': 'accountant', 'salary': '85'}]
dpts = [x['dpt'] for x in employees]
print(Counter(dpts))
# Counter({'tech': 2, 'accounting': 1})
试试这个:
def main():
duplicate = 0
employees = [
{'name': 'John', 'empID': '102', 'dpt': 'tech', 'title': 'programmer', 'salary': '75'},
{'name': 'Jane', 'empID': '202', 'dpt': 'tech', 'title': 'programmer', 'salary': '80'},
{'name': 'Joe', 'empID': '303', 'dpt': 'accounting', 'title': 'accountant', 'salary': '85'}
]
tech_dep = 0
accounting_dep = 0
for item in employees:
if item['dpt'] == 'tech':
tech_dep += 1
elif item['dpt'] == 'accounting':
accounting_dep += 1
print('Number of Employees from tech dep are : ', tech_dep)
print('Number of Employees from accounting dep are : ', accounting_dep)
main()
它会给你这样的输出:
Number of Employees from tech dep are : 2
Number of Employees from accounting dep are : 1
注:
这是对您问题的简单回答,因为正如您所说,您是 python 的新手,所以我只想给您简单的回答!另请注意,您在问题中创建字典的方式是错误的,您可以制作字典字典或字典列表。而且@Austin刚刚给你的答案也很简单,说明了counter的使用
希望对您有所帮助! :)
如果您正在寻找一个特定键的总数,您可以使用 .count()
list method 作为直接查询。
给定一个字典列表:
>>> employees = [{'name': 'John', 'empID': '102', 'dpt': 'tech', 'title': 'programmer', 'salary': '75'},
{'name': 'Jane', 'empID': '202', 'dpt': 'tech', 'title': 'programmer', 'salary': '80'},
{'name': 'Joe', 'empID': '303', 'dpt': 'accounting', 'title': 'accountant', 'salary': '85'}]
您可以使用列表理解创建每个列表:
>>> [d['dpt'] for d in employees]
['tech', 'tech', 'accounting']
然后可以统计:
>>> [d['dpt'] for d in employees].count('tech')
2
>>> [d['dpt'] for d in employees].count('accounting')
1
>>> [d['dpt'] for d in employees].count('nothing')
0
或者,您可以构造一个生成器来计算特定值:
>>> sum(1 for d in employees if d['dpt']=='tech')
2
如果你想获得所有元素的计数(除了使用计数器)你可以这样做:
>>> lot=[d['dpt'] for d in employees]
>>> {c:lot.count(c) for c in set(lot)}
{'accounting': 1, 'tech': 2}
python 对我来说是新语言,所以这个问题听起来很简单,但如果有人能指出正确的方向,我将不胜感激!我创建了一个名为 employees 的字典,它对他们有一些价值:
我想知道每个部门有多少人,例如:tech-2,accounting-1。
我有这样的东西,但它打印出来是空白的。
def main():
employees= {'name': 'John', 'empID': '102', 'dpt': 'tech', 'title':
'programmer', 'salary': '75'}
{'name': 'Jane', 'empID': '202', 'dpt': 'tech', 'title': 'programmer',
'salary': '80'}
{'name': 'Joe', 'empID': '303', 'dpt': 'accounting', 'title':
'accountant', 'salary': '85'}
for item in employees:
dic = employees[item]
if dic['dpt'[0]]==dic['dpt'[1]]:
duplicate += 1
print("there are "+ duplicate)
else:
print("There are no duplicate")
如果您不想导入 Counter
,请使用字典来跟踪每个部门的员工人数:
employee_list = [{
'name': 'John',
'empID': '102',
'dpt': 'tech',
'title': 'programmer',
'salary': '75'
}, {
'name': 'Jane',
'empID': '202',
'dpt': 'tech',
'title': 'programmer',
'salary': '80'
}, {
'name': 'Joe',
'empID': '303',
'dpt': 'accounting',
'title': 'accountant',
'salary': '85'
}]
department_to_count = dict()
for employee in employee_list:
department = employee["dpt"]
if department not in department_to_count:
department_to_count[department] = 0
department_to_count[department] += 1
for department, employee_count in department_to_count.items():
print("Department {} has {} employees".format(department,
employee_count))
from collections import Counter
employees = [{'name': 'John', 'empID': '102', 'dpt': 'tech', 'title': 'programmer', 'salary': '75'},
{'name': 'Jane', 'empID': '202', 'dpt': 'tech', 'title': 'programmer', 'salary': '80'},
{'name': 'Joe', 'empID': '303', 'dpt': 'accounting', 'title': 'accountant', 'salary': '85'}]
dpts = [x['dpt'] for x in employees]
print(Counter(dpts))
# Counter({'tech': 2, 'accounting': 1})
试试这个:
def main():
duplicate = 0
employees = [
{'name': 'John', 'empID': '102', 'dpt': 'tech', 'title': 'programmer', 'salary': '75'},
{'name': 'Jane', 'empID': '202', 'dpt': 'tech', 'title': 'programmer', 'salary': '80'},
{'name': 'Joe', 'empID': '303', 'dpt': 'accounting', 'title': 'accountant', 'salary': '85'}
]
tech_dep = 0
accounting_dep = 0
for item in employees:
if item['dpt'] == 'tech':
tech_dep += 1
elif item['dpt'] == 'accounting':
accounting_dep += 1
print('Number of Employees from tech dep are : ', tech_dep)
print('Number of Employees from accounting dep are : ', accounting_dep)
main()
它会给你这样的输出:
Number of Employees from tech dep are : 2
Number of Employees from accounting dep are : 1
注:
这是对您问题的简单回答,因为正如您所说,您是 python 的新手,所以我只想给您简单的回答!另请注意,您在问题中创建字典的方式是错误的,您可以制作字典字典或字典列表。而且@Austin刚刚给你的答案也很简单,说明了counter的使用
希望对您有所帮助! :)
如果您正在寻找一个特定键的总数,您可以使用 .count()
list method 作为直接查询。
给定一个字典列表:
>>> employees = [{'name': 'John', 'empID': '102', 'dpt': 'tech', 'title': 'programmer', 'salary': '75'},
{'name': 'Jane', 'empID': '202', 'dpt': 'tech', 'title': 'programmer', 'salary': '80'},
{'name': 'Joe', 'empID': '303', 'dpt': 'accounting', 'title': 'accountant', 'salary': '85'}]
您可以使用列表理解创建每个列表:
>>> [d['dpt'] for d in employees]
['tech', 'tech', 'accounting']
然后可以统计:
>>> [d['dpt'] for d in employees].count('tech')
2
>>> [d['dpt'] for d in employees].count('accounting')
1
>>> [d['dpt'] for d in employees].count('nothing')
0
或者,您可以构造一个生成器来计算特定值:
>>> sum(1 for d in employees if d['dpt']=='tech')
2
如果你想获得所有元素的计数(除了使用计数器)你可以这样做:
>>> lot=[d['dpt'] for d in employees]
>>> {c:lot.count(c) for c in set(lot)}
{'accounting': 1, 'tech': 2}