从重复项JSON

时间:2018-03-30 18:15:17

标签: python json dictionary

我有这个json派生的词典:

{
    "stats": [
        {
            "name": "Jengas",
            "time": 166,
            "uid": "177098244407558145",
            "id": 1
        },
        {
            "name": "- k",
            "time": 20,
            "uid": "199295228664872961",
            "id": 2
        },
        {
            "name": "MAD MARX",
            "time": "0",
            "uid": "336539711785009153",
            "id": 3
        },
        {
            "name": "loli",
            "time": 20,
            "uid": "366299640976375818",
            "id": 4
        },
        {
            "name": "Woona",
            "time": 20,
            "uid": "246996981178695686",
            "id": 5
        }
    ]
}

我希望得到"时间"来自列表中的每个人并使用它进行排序。

所以我得到的结果是:

TOP 10:
Jengas: 166
Loli: 20

我的第一次尝试是列出重复项目的不同值。

现在代码是:

with open('db.json') as json_data:
    topvjson = json.load(json_data)
    print(topvjson)
    d = topvjson['stats'][0]['time']
    print(d)

3 个答案:

答案 0 :(得分:4)

提取stats列表,使用相应的密钥将sort应用于该列表:

from json import loads

data = loads("""{
  "stats": [{
    "name": "Jengas",
    "time": 166,
    "uid": "177098244407558145",
    "id": 1
  }, {
    "name": "- k",
    "time": 20,
    "uid": "199295228664872961",
    "id": 2
  }, {
    "name": "MAD MARX",
    "time": "0",
    "uid": "336539711785009153",
    "id": 3
  }, {
    "name": "loli",
    "time": 20,
    "uid": "366299640976375818",
    "id": 4
  }, {
    "name": "Woona",
    "time": 20,
    "uid": "246996981178695686",
    "id": 5
  }]
}""")

stats = data['stats']
stats.sort(key = lambda entry: int(entry['time']), reverse=True)

print("TOP 10:")
for entry in stats[:10]:
  print("%s: %d" % (entry['name'], int(entry['time'])))     

打印:

TOP 10:
Jengas: 166
- k: 20
loli: 20
Woona: 20
MAD MARX: 0

请注意,您的time既不是整数也不是字符串:数据集中同时包含0"0"。这就是您需要转化int(...)

的原因

答案 1 :(得分:3)

您可以对dict值列表进行排序,例如:

代码:

top_three = [(x[1], -x[0]) for x in sorted(
    (-int(user['time']), user['name']) for user in stats['stats'])][:3]

这通过花时间和名称并构建元组来实现。可以对元组进行排序,然后在排序后提取名称(通过:x[1])。

测试代码:

stats = {
    "stats": [{
        "name": "Jengas",
        "time": 166,
        "uid": "177098244407558145",
        "id": 1
    }, {
        "name": "- k",
        "time": 20,
        "uid": "199295228664872961",
        "id": 2
    }, {
        "name": "MAD MARX",
        "time": "0",
        "uid": "336539711785009153",
        "id": 3
    }, {
        "name": "loli",
        "time": 20,
        "uid": "366299640976375818",
        "id": 4
    }, {
        "name": "Woona",
        "time": 20,
        "uid": "246996981178695686",
        "id": 5
    }]
}

top_three = [x[1] for x in sorted(
    (-int(user['time']), user['name']) for user in stats['stats'])][:3]

print(top_three)

结果:

[('Jengas', 166), ('- k', 20), ('Woona', 20)]

答案 2 :(得分:1)

以下是使用内置sorted()函数执行此操作的方法:

data = {
    "stats": [
        {
            "name": "Jengas",
            "time": 166,
            "uid": "177098244407558145",
            "id": 1
        },
        {   
           etc ...
        }
    ]
}

print('TOP 3')
sorted_by_time = sorted(data['stats'], key=lambda d: int(d['time']), reverse=True)

for i, d in enumerate(sorted_by_time, 1):
    if i > 3: break
    print('{name}: {time}'.format(**d))

输出:

TOP 3
Jengas: 166
- k: 20
loli: 20