python是我的新语言,所以这个问题可能听起来很简单,但如果有人能指出我正确的方向,我会很感激!我创建了一个字典呼叫员工,它对它们有一些价值:
我试图阅读每个部门中有多少人,例如: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")
答案 0 :(得分:2)
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})
答案 1 :(得分:0)
如果您不想导入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))
答案 2 :(得分:0)
试试这个:
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给你的答案也很简单,它显示了使用计数器。
希望这会对你有所帮助! :)
答案 3 :(得分:0)
如果您要查找一个特定密钥的总数,可以使用.count()
list method作为直接查询。
给出一个dicts列表:
>>> 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}