按顺序列出JSON列表?

时间:2018-06-03 17:26:23

标签: python json format

我有这份清单清单:

[['Edan Daniele', '12.61', '5.00', '9.22', '1.50', '60.39', '16.43', '21.60', '2.60', '35.81', '5.25.72', '3656'], 
['Lehi Poghos', '13.04', '4.53', '7.79', '1.55', '64.72', '18.74', '24.20', '2.40', '28.20', '6.50.76', '2696'], 
['Coos Kwesi', '13.75', '4.84', '10.12', '1.50', '68.44', '19.18', '30.85', '2.80', '33.88', '6.22.75', '2856'],
['Severi Eileifr', '13.43', '4.35', '8.64', '1.50', '66.06', '19.05', '24.89', '2.20', '33.48', '6.51.01', '2674']]

我想将此信息放入JSON格式/文件中。它应按升序顺序通过1到4,具体取决于列表的最后一个数字。

最好的方法是什么?

感谢您提前的时间! :)

1 个答案:

答案 0 :(得分:2)

new_dict = {}
for i, x in enumerate(sorted(data, key=lambda x: x[-1], reverse=True), 1):
    new_dict[str(i)] = {}
    new_dict[str(i)][x[0]] = x[1:]

print(new_dict)

<强>输出

{
    '1': {'Edan Daniele': ['12.61', '5.00', '9.22', '1.50', '60.39', '16.43', '21.60', '2.60', '35.81', '5.25.72', '3656']}, 
    '2': {'Coos Kwesi': ['13.75', '4.84', '10.12', '1.50', '68.44', '19.18', '30.85', '2.80', '33.88', '6.22.75', '2856']}, 
    '3': {'Lehi Poghos': ['13.04', '4.53', '7.79', '1.55', '64.72', '18.74', '24.20', '2.40', '28.20', '6.50.76', '2696']}, 
    '4': {'Severi Eileifr': ['13.43', '4.35', '8.64', '1.50', '66.06', '19.05', '24.89', '2.20', '33.48', '6.51.01', '2674']}
}