返回密钥:在密钥子密钥的子密钥上排序的子密钥对。蟒蛇

时间:2016-06-30 09:48:57

标签: python python-2.7 dictionary

我有一个JSON文件

 {
"b0:47:bf:af:c1:42": 
 {
 "No. of visits": 10, "cities": 
    {
      "Mumbai": {"count": 5,"last_visited": "5/22/2016"},
      "Kolkata": {"count": 2,"last_visited": "5/22/2016"},
      "Amritsar":{"count": 3,"last_visited": "5/22/2016"}
     }
},
"c0:ee:fb:71:be:0d": 
 {
 "No. of visits": 24, "cities": 
 {
  "Mumbai": {"count": 2,"last_visited": "5/22/2016"},
  "Kolkata": {"count": 20,"last_visited": "5/22/2016"},
  "Amritsar":{"count": 2,"last_visited": "5/22/2016"}
 }
}
}

因此,在解析为字典之后,我按照计数排序以获得每个mac的max_visited city,2nd max_visited city等等。

for mac in dic_data:
    sorted_cities = sorted(cities, key=lambda x: cities[x]['count'], reverse=True)

这给了我这样的输出。

['Kolkata', 'Amritsar', 'Mumbai']
['Amritsar', 'Mumbai', 'Kolkata']

这很好但是我可以说我想对整个字典进行“计数”排序并返回所有键,以便以后可以访问它。与此示例类似,"count"的最大值适用于"c0:ee:fb:71:be:0d"密钥"cities"子密钥和"Kolkata"进一步的子密钥。所以我想将它存储在一些我可以稍后调用它的结构中。 *我试图将它存储在数组*的数组中。我想稍后以dic array[0][1]访问它。我不确定是否可以这样做。我只需要稍后调用"Kolkata"及其"count"。  所以,我的输出可能看起来像

[["c0:ee:fb:71:be:0d","cities","Kolkata"],
 ["b0:47:bf:af:c1:42","cities","Mumbai"],
 ["b0:47:bf:af:c1:42","cities","Amritsar"],
 ["c0:ee:fb:71:be:0d","cities","Mumbai"],
 ["c0:ee:fb:71:be:0d","cities","Mumbai"],
 ["c0:ee:fb:71:be:0d","cities","Amritsar"],
 ["b0:47:bf:af:c1:42","cities","Kolkata"]]

1 个答案:

答案 0 :(得分:1)

你走了,这是我从问题,细节中理解的东西吗?请教!

>>> with open('file.txt', 'r') as f:
...     json_ = json.load(f)
>>> formatted_ = [[key, 'cities', city, value['cities'][city]['count']] for key, value in json_.items() for city in value['cities']]
>>> formatted_
185: [[u'b0:47:bf:af:c1:42', 'cities', u'Kolkata', 2],
 [u'b0:47:bf:af:c1:42', 'cities', u'Amritsar', 3],
 [u'b0:47:bf:af:c1:42', 'cities', u'Mumbai', 5],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Kolkata', 20],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Amritsar', 2],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Mumbai', 2]]
>>> sorted_ = sorted(formatted_, key=lambda x: x[3])
>>> sorted_
186: [[u'b0:47:bf:af:c1:42', 'cities', u'Kolkata', 2],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Amritsar', 2],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Mumbai', 2],
 [u'b0:47:bf:af:c1:42', 'cities', u'Amritsar', 3],
 [u'b0:47:bf:af:c1:42', 'cities', u'Mumbai', 5],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Kolkata', 20]]
>>> sorted_ = sorted(formatted_, key=lambda x: x[3], reverse=True)
>>> sorted_
187: [[u'c0:ee:fb:71:be:0d', 'cities', u'Kolkata', 20],
 [u'b0:47:bf:af:c1:42', 'cities', u'Mumbai', 5],
 [u'b0:47:bf:af:c1:42', 'cities', u'Amritsar', 3],
 [u'b0:47:bf:af:c1:42', 'cities', u'Kolkata', 2],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Amritsar', 2],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Mumbai', 2]]
>>> minimize_sorted_ = [item[:3] for item in sorted_]
>>> minimize_sorted_
188: [[u'c0:ee:fb:71:be:0d', 'cities', u'Kolkata'],
 [u'b0:47:bf:af:c1:42', 'cities', u'Mumbai'],
 [u'b0:47:bf:af:c1:42', 'cities', u'Amritsar'],
 [u'b0:47:bf:af:c1:42', 'cities', u'Kolkata'],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Amritsar'],
 [u'c0:ee:fb:71:be:0d', 'cities', u'Mumbai']]