2017-05-20 9 views
0

ここで私は3レベルで並べ替えたいjsonです辞書のリストで3レベルのソートを行う方法は?

例:最初の並べ替えremote_host、次にclient_ip、次にタイムスタンプ、そして1つのjsonで出力します。 3レベルでソートする必要があり、JSON上にある

data = [{"timestamp": 1495126917.0, "client_ip": "10.200.3.55", "remote_host": "175.106.59.78"}, 
     {"timestamp": 1495126915.0, "client_ip": "10.200.3.55", "remote_host": "175.106.59.78"}, 
     {"timestamp": 1495126917.0, "client_ip": "10.200.3.55", "remote_host": "201.83.41.11"}, 
     {"timestamp": 1495126913.0, "client_ip": "10.200.3.55", "remote_host": "78.47.139.58"}, 
     {"timestamp": 1495126915.0, "client_ip": "10.200.3.55", "remote_host": "175.106.59.78"}, 
     {"timestamp": 1495126917.0, "client_ip": "10.200.3.55", "remote_host": "201.83.41.11"}, 
     {"timestamp": 1495126913.0, "client_ip": "10.200.3.55", "remote_host": "201.83.41.11"}, 
     {"timestamp": 1495126915.0, "client_ip": "10.200.3.55", "remote_host": "78.47.139.58"}, 
     {"timestamp": 1495126913.0, "client_ip": "10.200.3.55", "remote_host": "175.106.59.78"}, 
     {"timestamp": 1495126915.0, "client_ip": "10.200.3.55", "remote_host": "201.83.41.11"}] 
+0

。 –

答えて

2
import json 
import operator 

data = [{"timestamp": 1495126917.0, "client_ip": "10.200.3.55", "remote_host": "175.106.59.78"}, {"timestamp": 1495126915.0, "client_ip": "10.200.3.55", "remote_host": "175.106.59.78"}, {"timestamp": 1495126917.0, "client_ip": "10.200.3.55", "remote_host": "201.83.41.11"}, {"timestamp": 1495126913.0, "client_ip": "10.200.3.55", "remote_host": "78.47.139.58"}, {"timestamp": 1495126915.0, "client_ip": "10.200.3.55", "remote_host": "175.106.59.78"}, {"timestamp": 1495126917.0, "client_ip": "10.200.3.55", "remote_host": "201.83.41.11"}, {"timestamp": 1495126913.0, "client_ip": "10.200.3.55", "remote_host": "201.83.41.11"}, {"timestamp": 1495126915.0, "client_ip": "10.200.3.55", "remote_host": "78.47.139.58"}, {"timestamp": 1495126913.0, "client_ip": "10.200.3.55", "remote_host": "175.106.59.78"}, {"timestamp": 1495126915.0, "client_ip": "10.200.3.55", "remote_host": "201.83.41.11"}] 
print json.dumps(sorted(data, key=operator.itemgetter('remote_host', 'client_ip', 'timestamp'))) 
関連する問題