Python 2.7 Python中的字典键重复(List<;int>;)#

Python 2.7 Python中的字典键重复(List<;int>;)#,python-2.7,list,dictionary,duplicates,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,

这是一项任务;我已经努力过了,在某个地方卡住了

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

    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
这是文本文件所需的输出:

    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
这是我对文本文件的处理:

    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
逻辑

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

        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])])
    
  • 从这里,我计算所需的值,并将结果写入文件

  • 我的问题;我怎样才能使最小值和最大值在字典中重复,正如您看到的那样,已被覆盖


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

    为了解决这个问题,我建议将结构类型从
    Dictionary
    更改为其他类型(如嵌套的
    列表

    您将得到一个行列表,每个行包含函数(如“min”)和数字数组


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

    为了解决这个问题,我建议将结构类型从
    Dictionary
    更改为其他类型(如嵌套的
    列表

    您将得到一个行列表,每个行包含函数(如“min”)和数字数组


    你有什么理由需要将这些项目存储在字典中吗?在我看来,你可以逐行阅读、计算并打印结果,就这样。。。你不需要担心重复的键…你有什么理由需要将这些项存储在字典中吗?在我看来,你可以逐行阅读,计算并打印结果,就这样。。。您不必担心重复的密钥。。。