您需要一个查找表来确定每个(ID, level)
元组的值。您需要将所有可能的级别查找添加到排序键:
# Lookup for values by (id, level)
values = {}
minlevel, maxlevel = float('inf'), float('-inf')
for id_, level, value in inputlist:
values[id_, level] = value
if level < minlevel:
minlevel = level
if level > maxlevel:
maxlevel = level
def level_sort(tup):
id_, level, value = tup
sortkey = [level]
for l in range(minlevel, maxlevel + 1):
sortkey.append(values.get((id_, l)) if l >= level else None)
return sortkey
sorted(inputlist, key=level_sort)
sort 函数生成要排序的值列表,从它们自己级别的值开始(填充None
任何较低级别)加上更高级别的相同 ID 的值。
例如,对于(B, 1, None)
和(C, 1, None)
元组,排序函数输出:
>>> level_sort(('B', 1, None))
[1, None, 5.0, 55]
>>> level_sort(('C', 1, None))
[1, None, 5.0, 40]
其中只有最后一个值不同并确定最终的排序顺序。
为此,您需要首先知道输入列表中的最小和最大级别,因此需要更精细的地图构建循环。
演示:
>>> from pprint import pprint
>>> A, B, C = 'ABC'
>>> inputlist = [(A, 1, None),
... (B, 1, None),
... (C, 1, None),
... (A, 2, 1.1),
... (B, 2, 5.0),
... (C, 2, 5.0),
... (C, 3, 40),
... (B, 3, 55)]
>>> values = {}
>>> minlevel, maxlevel = float('inf'), float('-inf')
>>> for id_, level, value in inputlist:
... values[id_, level] = value
... if level < minlevel:
... minlevel = level
... if level > maxlevel:
... maxlevel = level
...
>>> def level_sort(tup):
... id_, level, value = tup
... sortkey = [level]
... for l in range(minlevel, maxlevel + 1):
... sortkey.append(values.get((id_, l)) if l >= level else None)
... return sortkey
...
>>> pprint(sorted(inputlist, key=level_sort))
[('A', 1, None),
('C', 1, None),
('B', 1, None),
('A', 2, 1.1),
('C', 2, 5.0),
('B', 2, 5.0),
('C', 3, 40),
('B', 3, 55)]