字典键 - 在Python中重复(List <int>)#

时间:2016-11-18 11:52:17

标签: python-2.7 list dictionary duplicates

这是一项任务;我已经完成了它,某个地方卡住了;

这是来自文本文件的输入:

    min: 1,2,3,5,6
    max: 1,2,3,5,6
    avg: 1,2,3,5,6
    p90: 1,2,3,4,5,6,7,8,9,10
    sum: 1,2,3,5,6
    min: 1,5,6,14,24
    max: 2,3,9
    p70: 1,2,3

这是文本文件的必需输出:

    The min of [1, 2, 3, 5, 6] is 1
    The max of [1, 2, 3, 5, 6] is 6
    The avg of [1, 2, 3, 5, 6] is 3.4
    The 90th percentile of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is 9
    The sum of [1, 2, 3, 5, 6] is 17
    The min of [1, 5, 6, 14, 24] is 1
    The max of [2, 3, 9] is 9
    The 70th percentile of [1, 2, 3] is 2

这是我对文本文件的解决方法:

    The min of [1, 5, 6, 14, 24] is 1
    The max of [2, 3, 9] is 9
    The avg of [1, 2, 3, 5, 6] is 3.4
    The p90 of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is 9.0
    The sum of [1, 2, 3, 5, 6] is 17
    The p70 of [1, 2, 3] is 2.1

逻辑

  1. 我写了一个函数来读取文件并将键:值插入字典;
  2. 下面是字典

        OrderedDict([('min', [1, 5, 6, 14, 24]), ('max', [2, 3, 9]), ('avg', [1, 2, 3, 5, 6]), ('p90', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), ('sum', [1, 2, 3, 5, 6]), ('p70', [1, 2, 3])])
    
    1. 从这里我计算所需的并将结果写入文件

    2. 我的问题;如何在字典中使密钥最小和最大重复,因为您可以看到已被覆盖

1 个答案:

答案 0 :(得分:0)

问题是,字典中的键是唯一的。这意味着,字典只能有一个带有'min'键的条目。这就是为什么第一个带有'min'键的条目会被第二个条目覆盖。

要解决此问题,我建议将结构类型从Dictionary更改为其他内容(如嵌套List)。

list = []
list.append(['min', [1, 2, 3, 5, 6]])

您将获得一个列表,每个行包含该函数(如'min')和数字数组。

More about Lists