Python 是否按另一个列表的索引对列表进行排序?

Python 是否按另一个列表的索引对列表进行排序?,python,sorting,Python,Sorting,我有一本词典。在这些子词典中,我有两个键-ui\u section和section\u order,它们确定此子词典的值是否显示在ui的特定部分,如果是,它的显示顺序。我的字典是这样的: MASTER_DICT = { 'key1': {'ui_section':[1,2],'section_order':1, 'value': 'key1'}, 'key2': {'ui_section':[1],'section_order':2, 'value': 'key2'},

我有一本词典。在这些子词典中,我有两个键-
ui\u section
section\u order
,它们确定此子词典的
值是否显示在ui的特定部分,如果是,它的显示顺序。我的字典是这样的:

MASTER_DICT = {
    'key1': {'ui_section':[1,2],'section_order':1, 'value': 'key1'},
    'key2': {'ui_section':[1],'section_order':2, 'value': 'key2'},
    'key3': {'ui_section':[1,2],'section_order':3, 'value': 'key3'},
    'key4': {'ui_section':[1],'section_order':4, 'value': 'key4'},
    'key5': {'ui_section':[1],'section_order':5, 'value': 'key5'},
    'key6': {'ui_section':[1],'section_order':6, 'value': 'key6'},
    'key7': {'ui_section':[1],'section_order':7, 'value': 'key7'},
    'key8': {'ui_section':[1],'section_order':8, 'value': 'key8'},
    'key9': {'ui_section':[1],'section_order':9, 'value': 'key9'},
}
MASTER_DICT = {
    'key1': {'ui_section':[1,2],'section_order':[1,2], 'value': 'key1'},
    'key2': {'ui_section':[1],'section_order':[2], 'value': 'key2'},
    'key3': {'ui_section':[1,2],'section_order':[3,1], 'value': 'key3'},
}
 >>> pprint.pprint(show_section_ui(MASTER_DICT, 2))
 [{'section_order': [3,1], 'ui_section': [1,2], 'value': 'key3'},
 {'section_order': [1,2], 'ui_section': [1,2], 'value': 'key1'}]
ui\u部分
是键可能出现的部分列表。我通过以下代码确定这一点:

def show_section_ui(master_dict, section=None):
    if section:
        ui_sections = []
        # Find the keys that are part of this section
        for k in master_dict.keys():
            try:
                if section in master_dict[k]['ui_section']:
                    ui_sections.append(master_dict[k])
            except AttributeError:
                pass
        # Order the keys by sort order
        ui_sections.sort(key=lambda x: x['section_order'])

        return ui_sections
    else:
        return None
这部分代码是有效的。下面的输出表明,第1节和第2节的顺序都是正确的

>>> pprint.pprint(show_section_ui(MASTER_DICT, 1))
[{'section_order': 1, 'ui_section': [1,2], 'value': 'key1'},
 {'section_order': 2, 'ui_section': [1], 'value': 'key2'},
 {'section_order': 3, 'ui_section': [1,2], 'value': 'key3'},
 {'section_order': 4, 'ui_section': [1], 'value': 'key4'},
 {'section_order': 5, 'ui_section': [1], 'value': 'key5'},
 {'section_order': 6, 'ui_section': [1], 'value': 'key6'},
 {'section_order': 7, 'ui_section': [1], 'value': 'key7'},
 {'section_order': 8, 'ui_section': [1], 'value': 'key8'},
 {'section_order': 9, 'ui_section': [1], 'value': 'key9'}]


 >>> pprint.pprint(show_section_ui(MASTER_DICT, 2))
 [{'section_order': 1, 'ui_section': [1,2], 'value': 'key1'},
  {'section_order': 3, 'ui_section': [1,2], 'value': 'key3'}]
我的问题是,
section\u order
需要按照
ui\u section
进行排序。例如,在上面的输出中,在第2节中,我希望键3是第一个。我最初的想法是把
section\u order
也列成一个列表。但是,我不知道如何调整这一行以正确地说明列表(并选择正确的索引进行排序)

我的意图是这样做:

MASTER_DICT = {
    'key1': {'ui_section':[1,2],'section_order':1, 'value': 'key1'},
    'key2': {'ui_section':[1],'section_order':2, 'value': 'key2'},
    'key3': {'ui_section':[1,2],'section_order':3, 'value': 'key3'},
    'key4': {'ui_section':[1],'section_order':4, 'value': 'key4'},
    'key5': {'ui_section':[1],'section_order':5, 'value': 'key5'},
    'key6': {'ui_section':[1],'section_order':6, 'value': 'key6'},
    'key7': {'ui_section':[1],'section_order':7, 'value': 'key7'},
    'key8': {'ui_section':[1],'section_order':8, 'value': 'key8'},
    'key9': {'ui_section':[1],'section_order':9, 'value': 'key9'},
}
MASTER_DICT = {
    'key1': {'ui_section':[1,2],'section_order':[1,2], 'value': 'key1'},
    'key2': {'ui_section':[1],'section_order':[2], 'value': 'key2'},
    'key3': {'ui_section':[1,2],'section_order':[3,1], 'value': 'key3'},
}
 >>> pprint.pprint(show_section_ui(MASTER_DICT, 2))
 [{'section_order': [3,1], 'ui_section': [1,2], 'value': 'key3'},
 {'section_order': [1,2], 'ui_section': [1,2], 'value': 'key1'}]
让我像这样输出:

MASTER_DICT = {
    'key1': {'ui_section':[1,2],'section_order':1, 'value': 'key1'},
    'key2': {'ui_section':[1],'section_order':2, 'value': 'key2'},
    'key3': {'ui_section':[1,2],'section_order':3, 'value': 'key3'},
    'key4': {'ui_section':[1],'section_order':4, 'value': 'key4'},
    'key5': {'ui_section':[1],'section_order':5, 'value': 'key5'},
    'key6': {'ui_section':[1],'section_order':6, 'value': 'key6'},
    'key7': {'ui_section':[1],'section_order':7, 'value': 'key7'},
    'key8': {'ui_section':[1],'section_order':8, 'value': 'key8'},
    'key9': {'ui_section':[1],'section_order':9, 'value': 'key9'},
}
MASTER_DICT = {
    'key1': {'ui_section':[1,2],'section_order':[1,2], 'value': 'key1'},
    'key2': {'ui_section':[1],'section_order':[2], 'value': 'key2'},
    'key3': {'ui_section':[1,2],'section_order':[3,1], 'value': 'key3'},
}
 >>> pprint.pprint(show_section_ui(MASTER_DICT, 2))
 [{'section_order': [3,1], 'ui_section': [1,2], 'value': 'key3'},
 {'section_order': [1,2], 'ui_section': [1,2], 'value': 'key1'}]

如何按
ui\u部分和键中的适当索引进行排序?

我想我明白了。如果要根据另一个项目列表的顺序对项目列表进行排序,可以执行以下操作。一步一步,最后一行是你需要的

我们需要
itertools.count()
,这是一个无限递增的范围,用于应用索引

import itertools
这些应按“值”排序

>>> values = [{"name": "A", "value": 10},
              {"name": "B", "value": 8},
              {"name": "C", "value": 9}]
这些值的输入顺序与
值的输入顺序相同,并且应按照相同的顺序进行排序

>>> to_sort = [{"name": "A", "payload": "aaa"},
               {"name": "B", "payload": "bbb"},
               {"name": "C", "payload": "ccc"}]
压缩值及其索引。这将注释每个项目以包括其原始顺序。这是(对象、索引)对的列表

现在按键“value”排序。
x[0]
用于获取对中的第一项(对象)

现在从索引对中检索索引,以返回原始索引的新顺序

>>> map(lambda x: x[1],
        sorted(zip(values, itertools.count()),
               key=lambda x: x[0]["value"]))
[1, 2, 0]
现在使用这些索引映射到to_排序列表并检索这些索引处的项

>>> map(lambda i: to_sort[i],
        map(lambda x: x[1],
            sorted(zip(values, itertools.count()),
                   key=lambda x: x[0]["value"])))

[{'name': 'B', 'payload': 'bbb'}, 
 {'name': 'C', 'payload': 'ccc'},
 {'name': 'A', 'payload': 'aaa'}]

我希望这能回答你的问题。这意味着将主目录更改为列表,但我认为这是最好的表示方式。

我没有一行代码可以为您更改,但您可以替换您的行:

ui_sections.sort(key=lambda x: x['section_order'])
为此:

sort_orders = []
for s in ui_sections:
    ndx = s['ui_section'].index(section)
    # This next line makes the assumption that you ALWAYS have a section_order
    # for every ui_section listed. If not, you'll get an IndexError
    sort_orders.append(s['section_order'][ndx])

    # Magic happens here        
    sorted_sections = [x for y, x in sorted(zip(sort_orders,ui_sections))]
    return sorted_sections

输出:

>>> pprint.pprint(show_section_ui(MASTER_DICT, 2))
[{'section_order': [3, 1], 'ui_section': [1, 2], 'value': 'key3'},
 {'section_order': [1, 2], 'ui_section': [1, 2], 'value': 'key1'}]

>>> pprint.pprint(show_section_ui(MASTER_DICT, 1))
[{'section_order': [1, 2], 'ui_section': [1, 2], 'value': 'key1'},
 {'section_order': [2], 'ui_section': [1], 'value': 'key2'},
 {'section_order': [3, 1], 'ui_section': [1, 2], 'value': 'key3'},
 {'section_order': [4], 'ui_section': [1], 'value': 'key4'},
 {'section_order': [5], 'ui_section': [1], 'value': 'key5'},
 {'section_order': [6], 'ui_section': [1], 'value': 'key6'},
 {'section_order': [7], 'ui_section': [1], 'value': 'key7'},
 {'section_order': [8], 'ui_section': [1], 'value': 'key8'},
 {'section_order': [9], 'ui_section': [1], 'value': 'key9'}]
key8
添加到位置3的第二个
ui\u部分,并将
key7
添加到位置4:

[{'section_order': [3, 1], 'ui_section': [1, 2], 'value': 'key3'},
 {'section_order': [1, 2], 'ui_section': [1, 2], 'value': 'key1'},
 {'section_order': [8, 3], 'ui_section': [1, 2], 'value': 'key8'},
 {'section_order': [7, 4], 'ui_section': [1, 2], 'value': 'key7'}]

这是我的答案。但是,首先,它会找到该节在
ui\u节中列出的索引:

ndx = s['ui_section'].index(section)
然后将此位置的值添加到
sort\u orders
列表中。请注意,所提供的代码不会错误检查该值是否有效(即,如果您没有第二个位置的值),如果没有,则将抛出
索引器

sort_orders.append(s['section_order'][ndx])
接下来,它将两个列表压缩在一起,这样就有了一个元组列表,其中包含排序顺序和节字典

[(3, {'ui_section': [1, 2], 'section_order': [8, 3], 'value': 'key8'}), 
 (1, {'ui_section': [1, 2], 'section_order': [3, 1], 'value': 'key3'}), 
 (2, {'ui_section': [1, 2], 'section_order': [1, 2], 'value': 'key1'}), 
 (4, {'ui_section': [1, 2], 'section_order': [7, 4], 'value': 'key7'})
]
然后我们根据元组中的第一个位置对其进行排序。然后我们将其解压并提取已排序的信息。所有这些都发生在这一行:

sorted_sections = [x for y, x in sorted(zip(sort_orders,ui_sections))]