czy_1996
the_passenger
采纳率0%
2019-02-28 20:02 阅读 5.9k

python3 sorted()函数报错 TypeError: '<' not supported between instances of 'str' and 'int'

想对dict按值的大小进行排序。
我不明白的是,我明明在前面把所有非整形都转成整形了,它还是报类型错误。

dict = {}
for word in words:
    word=word.replace("\n","")
    body = {"query":{"bool":{"must":[{"range":{"time":{"gt":"2018-01-01","lt":"2019-01-01"}}},{"term":{"detailwords": word}}],"must_not":[],"should":[]}},"from":0,"size":20,"sort":[{"hot":"desc"}],"aggs":{}}
    res = es.search(index="events_v3", body=body)
    # print(res['hits']['hits'])
    for item in res['hits']['hits']:
        dict[item['_source']['desc']] = item['_source']['hot']

for key, value in dict.items():
    if type(value) != type(100):
        print("a")
        dict[key] = int(value)

    list1 = sorted(dict.values())

filename = 'abcd.json'
with open(filename, 'w', encoding='UTF-8') as f_obj1:
    json.dump(dict, f_obj1, ensure_ascii=False)

报的错误是:

Traceback (most recent call last):
File "G:/pycharm_workspace/ESDemo/demo1.py", line 24, in
list1 = sorted(dict.values())
TypeError: '<' not supported between instances of 'str' and 'int'

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐