Python-对列表的字典中的值进行排序

时间:2019-06-14 21:57:48

标签: python list sorting dictionary

一组13个传感器传输每5分钟读取一次的数值数据。为了捕获这些值,我使用了列表字典:

Sensors = {
         'Sensor1':[10.52,12.45,15.70],
         'Sensor5':[12.32,22.80,30.42],
         'Sensor3':[4.7,3.4,2.1],
         'Sensor8':[2.34,4.28,7.10],
         'Sensor4':[10.64,14.76,4.25],
         'Sensor2':[3.21,7.88,9.22],
         'Sensor6':[4.11,9.32,2.70],
         'Sensor9':[11.45,14.72,8.30],
         'Sensor7':[6.10,3.98,10.66],
         'Sensor10':[7.22,8.67,10.99],
         'Sensor13':[1.19,4.65,0.87],
         'Sensor12':[2.10,5.46,3.21],
         'Sensor11':[5.80,8.22,14.39]
         }

我需要对这个列表进行排序,以便使数量增加了正数的传感器出现在顶部。 为了实现此目标,我创建了另一个词典 DeltaValues ,其中包含传感器的名称以及捕获的最后一个值与第一个值之间的差异:

 DeltaValues={}
  for x, y in Sensors.items():
      if len(y) > 1:
         DeltaValues[x] = format(y[-1] - y[0],'.2f')
      else:
         DeltaValues[x] = format(y[0],'.2f')

我使用以下语法进行排序:

sorted_d = {k: v for k, v in sorted(DeltaValues.items(), key=lambda x: x[1])}

但是结果不是我所期望的:

print(sorted_d)

{'Sensor13': '-0.32',
 'Sensor6': '-1.41',
 'Sensor3': '-2.60',
 'Sensor9': '-3.15',
 'Sensor4': '-6.39',
 'Sensor12': '1.11',
 'Sensor5': '18.10',
 'Sensor10': '3.77',
 'Sensor7': '4.56',
 'Sensor8': '4.76',
 'Sensor1': '5.18',
 'Sensor2': '6.01',
 'Sensor11': '8.59'}

4 个答案:

答案 0 :(得分:2)

标识符不保证按其放入顺序返回值。

考虑订购字典。 https://docs.python.org/3/library/collections.html#collections.OrderedDict

答案 1 :(得分:2)

您可以对(key, value)对进行排序并将它们保留在列表中:

>>> sorted(Sensors.items(), key= lambda sensor:sensor[1][-1] - sensor[1][0])
[('Sensor4', [10.64, 14.76, 4.25]), ('Sensor9', [11.45, 14.72, 8.3]), ('Sensor3', [4.7, 3.4, 2.1]), ('Sensor6', [4.11, 9.32, 2.7]), ('Sensor13', [1.19, 4.65, 0.87]), ('Sensor12', [2.1, 5.46, 3.21]), ('Sensor10', [7.22, 8.67, 10.99]), ('Sensor7', [6.1, 3.98, 10.66]), ('Sensor8', [2.34, 4.28, 7.1]), ('Sensor1', [10.52, 12.45, 15.7]), ('Sensor2', [3.21, 7.88, 9.22]), ('Sensor11', [5.8, 8.22, 14.39]), ('Sensor5', [12.32, 22.8, 30.42])]

答案 2 :(得分:1)

这是解决方案

import operator
def getSortedSensorData():
    sensors = {
         'Sensor1':[10.52,12.45,15.70],
         'Sensor5':[12.32,22.80,30.42],
         'Sensor3':[4.7,3.4,2.1],
         'Sensor8':[2.34,4.28,7.10],
         'Sensor4':[10.64,14.76,4.25],
         'Sensor2':[3.21,7.88,9.22],
         'Sensor6':[4.11,9.32,2.70],
         'Sensor9':[11.45,14.72,8.30],
         'Sensor7':[6.10,3.98,10.66],
         'Sensor10':[7.22,8.67,10.99],
         'Sensor13':[1.19,4.65,0.87],
         'Sensor12':[2.10,5.46,3.21],
         'Sensor11':[5.80,8.22,14.39]
         }

    sensorData = {};

    for sensor in sensors:
        data = sensors[sensor]
        data = sorted(data, reverse = True)
        sensorData[sensor] = data[0];

    sensorData = sorted( sensorData.items(), key=operator.itemgetter(1),reverse=True ) 
    print( sensorData )

输出

  

[('Sensor5',30.42),('Sensor1',15.7),('Sensor4',14.76),   ('Sensor9',14.72),('Sensor11',14.39),('Sensor10',10.99),   ('Sensor7',10.66),('Sensor6',9.32),('Sensor2',9.22),('Sensor8',   7.1),('Sensor12',5.46),('Sensor3',4.7),('Sensor13',4.65)]

答案 3 :(得分:1)

如果您使用的是python 3.5+,请按如下所示使用常规dict

dict(sorted(Sensors.items(), key=lambda x: x[1][-1] - x[1][0], reverse=True))

Out[11]:
{'Sensor5': [12.32, 22.8, 30.42],
 'Sensor11': [5.8, 8.22, 14.39],
 'Sensor2': [3.21, 7.88, 9.22],
 'Sensor1': [10.52, 12.45, 15.7],
 'Sensor8': [2.34, 4.28, 7.1],
 'Sensor7': [6.1, 3.98, 10.66],
 'Sensor10': [7.22, 8.67, 10.99],
 'Sensor12': [2.1, 5.46, 3.21],
 'Sensor13': [1.19, 4.65, 0.87],
 'Sensor6': [4.11, 9.32, 2.7],
 'Sensor3': [4.7, 3.4, 2.1],
 'Sensor9': [11.45, 14.72, 8.3],
 'Sensor4': [10.64, 14.76, 4.25]}

如果您使用的是python <3.5+,请使用collecions.OrderedDict,如下所示:

collections.OrderedDict(sorted(Sensors.items(), key=lambda x: x[1][-1] - x[1][0], reverse=True))

Out[2067]:
OrderedDict([('Sensor5', [12.32, 22.8, 30.42]),
             ('Sensor11', [5.8, 8.22, 14.39]),
             ('Sensor2', [3.21, 7.88, 9.22]),
             ('Sensor1', [10.52, 12.45, 15.7]),
             ('Sensor8', [2.34, 4.28, 7.1]),
             ('Sensor7', [6.1, 3.98, 10.66]),
             ('Sensor10', [7.22, 8.67, 10.99]),
             ('Sensor12', [2.1, 5.46, 3.21]),
             ('Sensor13', [1.19, 4.65, 0.87]),
             ('Sensor6', [4.11, 9.32, 2.7]),
             ('Sensor3', [4.7, 3.4, 2.1]),
             ('Sensor9', [11.45, 14.72, 8.3]),
             ('Sensor4', [10.64, 14.76, 4.25])])