【问题标题】:How to fill dictionaries values by using the for loop?如何使用 for 循环填充字典值?
【发布时间】:2020-05-30 16:47:01
【问题描述】:

我有三个列表,每个列表都包含员工的唯一 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 个 bug、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 循环中的两次打印显示了正确的值。 任何想法,我做错了什么?

感谢帮助,问题已解决。

【问题讨论】:

  • 你只设置了all_employees[i][0] - 你从来没有设置all_employees[i][1] = task_d[i] or all_employees[i][2] = subtask_d[i]
  • 你为什么要except Exception as e: pass ?? dict(Counter(bug)) 的目的是什么? collections.Counter 不是 dict 的子类吗?

标签: python list dictionary for-loop


【解决方案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)

【讨论】:

    【解决方案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]}
    

    【讨论】:

      【解决方案3】:

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

      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]}
      

      【讨论】:

      • 在循环中使用list.count方法不必要地使其时间复杂度O(n ^ 2)
      • @blhsing 你能提出更好的方法吗?
      • 使用collections.Counter将列表转换为频率字典,以便您可以以O(1)时间复杂度查找每个ID的计数,从而导致时间复杂度为O(n) 总体。
      • 不错。不知道collections.Counter。谢谢!!
      【解决方案4】:

      您可以使用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]}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        • 2021-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多