难以理解如何使用Lambda对dict进行排序

时间:2018-10-23 00:16:37

标签: python sorting dictionary lambda

我正在尝试对这本词典进行排序。

d = {'FNP': ['0.02', '0.02', '0.02', '0.02'],
     'TestName': ['Test1205', 'Test1206', 'Test1207', 'Test1208'],
     'eno': ['0', '0', '0', '0'],
     'GRE': ['0.00', '0.00', '0.00', '0.00'],
     'TPS': ['78.00', '45.00', '73400', '34.00'],
     'id': ['1', '1', '1', '1']}

我想做的是基于TPS排序并输出名称wrt排序值。在此,TPS [0]对应于TesTName [0]。我想对TPS排序时对Testname排序。 预期输出:

d = {'TestName': ['Test1208','Test1206','Test1205','Test1207'],
     'TPS':['34.00', '45.00', '78.00', '73400']

这是我到目前为止的内容:sorted(d['TPS'],key=lambda d:d)

当我对Testname进行排序时,如何确保TPS也可以互换?

4 个答案:

答案 0 :(得分:1)

这是一个实用的解决方案。诀窍是使用enumerate提取索引,记住转换为float进行排序,并使用带有多个参数的itemgetter提取正确的顺序。

from operator import itemgetter

idx, _ = zip(*sorted(enumerate(d['TPS']), key=lambda x: float(x[1])))

res = {k: itemgetter(*idx)(d[k]) for k in ('TestName', 'TPS')}

{'TestName': ('Test1208', 'Test1206', 'Test1205', 'Test1207'),
 'TPS': ('34.00', '45.00', '78.00', '73400')}

答案 1 :(得分:0)

假设您希望TPS列表以数字方式 排序,而不是按词法排序(按字母顺序按其中每个字符串元素的值排序):

from pprint import pprint

d = {'FNP': ['0.02', '0.02', '0.02', '0.02'],
     'TestName': ['Test1205', 'Test1206', 'Test1207', 'Test1208'],
     'eno': ['0', '0', '0', '0'],
     'GRE': ['0.00', '0.00', '0.00', '0.00'],
     'TPS': ['78.00', '45.00', '73400', '34.00'],
     'id': ['1', '1', '1', '1']}

d['TestName'] = sorted(d['TestName'])
d['TPS'] = sorted(d['TPS'], key=float)

pprint(d)

输出:

{'FNP': ['0.02', '0.02', '0.02', '0.02'],
 'GRE': ['0.00', '0.00', '0.00', '0.00'],
 'TPS': ['34.00', '45.00', '78.00', '73400'],
 'TestName': ['Test1205', 'Test1206', 'Test1207', 'Test1208'],
 'eno': ['0', '0', '0', '0'],
 'id': ['1', '1', '1', '1']}

请注意,TestName列表已经排序。

答案 2 :(得分:0)

除了@jpp的解决方案之外,如果您想要新dict中原始dict中所有列表的排序版本,则可以使用原始TPS压缩每个数据列表,对元组进行排序,并从中创建新数据列表排序的数据:

original_tps = [float(t) for t in d['TPS']]
sorted_d = {}

for k, v in d.items():
    sorted_d[k] = [x for tps, x in sorted([(t, e) for t, e in zip(original_tps, v)])]


print(sorted_d['TPS'])
# ['34.00', '45.00', '78.00', '73400']                                                                                                    
print(sorted_d['TestName'])
# ['Test1208', 'Test1206', 'Test1205', 'Test1207']
# ... similarly other lists are sorted

答案 3 :(得分:0)

我将创建一个Record命名元组来保存每条记录,然后轻松排序并根据需要强制返回列表。

from collections import namedtuple

Record = namedtuple("Record", "FNP TestName eno GRE TPS id")

def extract(d, keys=None):
    if keys is None:
        keys = ["FNP", "TestName", "eno", "GRE", "TPS", "id"]
    records = [Record(*vals) for vals in zip(*map(d.get, keys))]
    return records

def transform(records):
    records.sort(key=lambda r: float(r.TPS))

def load(records, keys=None):
    if keys is None:
        keys = ["FNP", "TestName", "eno", "GRE", "TPS", "id"]
    d = {k: [getattr(record, k) for record in records] for k in keys}
    return d

d = {'FNP': ['0.02', '0.02', '0.02', '0.02'],
     'TestName': ['Test1205', 'Test1206', 'Test1207', 'Test1208'],
     'eno': ['0', '0', '0', '0'],
     'GRE': ['0.00', '0.00', '0.00', '0.00'],
     'TPS': ['78.00', '45.00', '73400', '34.00'],
     'id': ['1', '1', '1', '1']}
records = extract(d)
transform(records)
new_d = load(records)