如何使用for循环填充字典值?

时间:2020-02-14 21:40:37

标签: python list dictionary for-loop

我有三个列表,每个列表包含该员工的唯一ID。例如,

bug = [1,2,3,4,4,4,4,4,5,6,7,8,8,8]
task = [1,1,1,1,2,9]
subtask = [10, 11, 11, 11, 8, 8, 8, 8 ,8 , 8, 6]

员工ID的引用数显示员工已解决了多少个问题。例如,ID = 1的员工解决了1个错误,4个任务和0个子任务。我想创建一个具有以下结构的字典:键是员工的ID,值是大小为3的列表,列表的每个值都显示ID为=解决问题的员工有多少错误,任务和子任务。例如,就我而言,我必须获得下一个字典:

all_employees = {1:[1, 4, 0], 2:[1, 1, 0], 3:[1, 0, 0], 4:[5, 0, 0], 5:[1, 0, 0], 6:[1, 0, 1], \
                 7:[1, 0, 1], 8:[3, 0, 6], 9:[0, 1, 0], 10:[0, 0, 1], 11:[0, 0, 3]} 

这是我的代码:

from collections import defaultdict, Counter
bug = [1,2,3,4,4,4,4,4,5,6,7,8,8,8]
task = [1,1,1,1,2,9]
subtask = [10, 11, 11, 11, 8, 8, 8, 8 ,8 , 8, 6]
all_issues = bug + task + subtask
all_emlpoyees = dict.fromkeys(all_issues, [0, 0, 0])    
bug_d = dict(Counter(bug))
task_d = dict(Counter(task))
subtask_d = dict(Counter(subtask))
for i in all_employees.keys():
    try:
        print("value = ", bug_d[i])
        print ("key = ", i)

        all_employees[i][0] = bug_d[i]
    except Exception as e:
        pass
print (all_assignees)

我认为,此代码应产生相同的字典,如上所述,但其列表中仅包含一个第一个非零值,但我的输出看起来像:

{1: [3, 0, 0], 2: [3, 0, 0], 3: [3, 0, 0], 4: [3, 0, 0], 5: [3, 0, 0], 6: [3, 0, 0], 7: [3, 0, 0], 8: [3, 0, 0], 9: [3, 0, 0], 10: [3, 0, 0], 11: [3, 0, 0]}
尽管在for循环中有两次打印,

仍显示正确的值。 有什么想法,我在做什么错了?

感谢帮助,问题已解决。

4 个答案:

答案 0 :(得分:2)

您可以先将列表映射为Counter的列表,以便可以通过遍历一组所有键来创建所需的字典:

counts = list(map(Counter, (bug, task, subtask)))
all_employees = {id: [c[id] for c in counts] for id in {id for c in counts for id in c}}

all_employees变为:

{1: [1, 4, 0], 2: [1, 1, 0], 3: [1, 0, 0], 4: [5, 0, 0], 5: [1, 0, 0], 6: [1, 0, 1], 7: [1, 0, 0], 8: [3, 0, 6], 9: [0, 1, 0], 10: [0, 0, 1], 11: [0, 0, 3]}

答案 1 :(得分:1)

您可以使用Counter从每个列表中进行计数,然后创建字典。
这样可以避免计算每个员工的任务数量。

>>> c1, c2, c3 = Counter(bug), Counter(task), Counter(subtask) 
>>> d={}
>>> for employee in set(c1)|set(c2)|set(c3):
...     d[employee] = [c1[employee], c2[employee], c3[employee]]
... 
>>> d
{1: [1, 4, 0], 2: [1, 1, 0], 3: [1, 0, 0], 4: [5, 0, 0], 5: [1, 0, 0], 6: [1, 0, 1], 7: [1, 0, 0], 8: [3, 0, 6], 9: [0, 1, 0], 10: [0, 0, 1], 11: [0, 0, 3]}

答案 2 :(得分:1)

所需的输出可以使用以下代码来实现。

bug = [1, 2, 3, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 8]
task = [1, 1, 1, 1, 2, 9]
subtask = [10, 11, 11, 11, 8, 8, 8, 8, 8, 8, 6]

# first create a set of all employee id , no repeats
all_employees = set(bug + task + subtask)

# loop over each id and find the count of bug, task, sub_task and append it in the dict
final_dict = {}
for id in all_employees:
    final_dict[id] = [bug.count(id), task.count(id), subtask.count(id)]

print(final_dict)

输出:

{1: [1, 4, 0], 2: [1, 1, 0], 3: [1, 0, 0], 4: [5, 0, 0], 5: [1, 0, 0], 6: [1, 0, 1], 7: [1, 0, 0], 8: [3, 0, 6], 9: [0, 1, 0], 10: [0, 0, 1], 11: [0, 0, 3]}

答案 3 :(得分:1)

这是一行代码:

bug = [1,2,3,4,4,4,4,4,5,6,7,8,8,8]
task = [1,1,1,1,2,9]
subtask = [10, 11, 11, 11, 8, 8, 8, 8 ,8 , 8, 6]

{i:[bug.count(i), task.count(i), subtask.count(i)] for i in set(bug+task+subtask)}

Out[1]:
{1: [1, 4, 0],
 2: [1, 1, 0],
 3: [1, 0, 0],
 4: [5, 0, 0],
 5: [1, 0, 0],
 6: [1, 0, 1],
 7: [1, 0, 0],
 8: [3, 0, 6],
 9: [0, 1, 0],
10: [0, 0, 1],
11: [0, 0, 3]}

就像使用循环一样:

for i in set(bug+task+subtask):
    a[i] = [bug.count(i), task.count(i), subtask.count(i)]

print(a)