Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表的排序字典,其排序方式为值列表的第i个索引最大的项_Python_Python 3.x_List_Sorting_Dictionary - Fatal编程技术网

Python 列表的排序字典,其排序方式为值列表的第i个索引最大的项

Python 列表的排序字典,其排序方式为值列表的第i个索引最大的项,python,python-3.x,list,sorting,dictionary,Python,Python 3.x,List,Sorting,Dictionary,在python 3中工作时,我遇到以下问题: 我的字典由单位(键)和活动列表(值)组成,我必须对它进行排序,以便按峰值活动进行排序。这意味着我想找到每个索引I,该dict项的第I个值是值列表中的最高值 example_dict = {unit1: [1, 4, 3], unit2: [2, 2, 2], unit3: [1, 1, 1]} sorted_dict = {unit2: [2, 2, 2], unit1: [1, 4, 3], unit3: [1, 1, 1]} 我担心这可

在python 3中工作时,我遇到以下问题:

我的字典由单位(键)和活动列表(值)组成,我必须对它进行排序,以便按峰值活动进行排序。这意味着我想找到每个索引I,该dict项的第I个值是值列表中的最高值

example_dict = {unit1: [1, 4, 3], unit2: [2, 2, 2], unit3:  [1, 1, 1]}

sorted_dict  = {unit2: [2, 2, 2], unit1: [1, 4, 3], unit3:  [1, 1, 1]}
我担心这可能是一种情况,即本身没有一个最佳的解决方案,在这种情况下,我很高兴有一个任意选择的解决方案

  • 对于每个列表,找到最大元素的索引
  • 按该索引对列表进行分组
  • 从每个组中选择一个任意元素,并将其放入结果中的正确索引处
  • 使用剩余的元素填充结果中的空槽
  • 我使用的有用函数和类:

    • 分组
    • 存储结果
    • 作为
    • 并从dict中获取任意元素

    希望我举的例子能有所帮助!我原以为我理解这个问题,但现在你已经添加了你的示例,我意识到我不知道输出是如何生成的。你是说你不明白我想要什么?或者你不知道答案?我是说我不明白这个问题。输出看起来像是要按dict键的值对其进行排序,但在您的问题中,您要查找每个索引的最大值。因此在本例中,unit2排在顶部,因为所有值都是2,所以第一个值:2也是最高值。然后,因为在第二个位置unit1的值最高(4>1,3),所以该项位于第二位。我想有人会说unit3也可以去那里(因为1是unit3的最高值),但我更希望unit2排在第二位,因为4>1。非常感谢!我相信它能完全按照需要工作!
    import operator
    import collections
    
    example_dict = {'unit1': [1, 4, 3],
                    'unit2': [2, 2, 2],
                    'unit3': [1, 1, 1]}
    
    # group the lists by the index of their maximum element
    lists_by_max_index = collections.defaultdict(list)
    
    for key, values in example_dict.items():
        # find the index of the maximum element
        max_index, max_value = max(enumerate(values), key=operator.itemgetter(1))
        # and store the key in the corresponding group
        lists_by_max_index[max_index].append(key)
    
    # list_by_max_index is now {1: ['unit1'], 0: ['unit2', 'unit3']}
    
    # make a list representing the sorted dict and initialize it to None
    sorted_keys = [None] * len(next(iter(example_dict.values())))
    unused_keys = []
    
    # sorted_keys is [None, None, None]
    
    # for each group in the dict we created earlier, put a random key into
    # the list
    for i, keys in lists_by_max_index.items():
        sorted_keys[i] = keys.pop()
        unused_keys += keys  # store the other keys for later
    
    # sorted_keys is now ['unit3', 'unit1', None]
    
    # iterate over the list and fill any empty slots with arbitrary unused keys
    for i, key in enumerate(sorted_keys):
        if key is None:
            sorted_keys[i] = unused_keys.pop()
    
    # sorted_keys is now ['unit3', 'unit1', 'unit2']
    
    # finally, grab the corresponding value for each key and turn the whole thing
    # into an OrderedDict
    sorted_dict = [(key, example_dict[key]) for key in sorted_keys]
    sorted_dict = collections.OrderedDict(sorted_dict)
    
    print(sorted_dict)
    # output:
    # OrderedDict([('unit3', [1, 1, 1]),
    #              ('unit1', [1, 4, 3]),
    #              ('unit2', [2, 2, 2])])