如何按字母顺序输出标题?我想要它打印'娱乐'然后'信息'。
注意:这只是一个示例。我正在使用的json文件有更多的标题
>>> import json
>>> field = json.loads('{"js":[{"id":"1","title":"information","number":"1","alias":"information"},{"id":"2","title":"entertainments","number":"2","alias":"entertainments"}],"text":""}')
>>> for data in field['js']:
... print data['title']
...
information
entertainments
答案 0 :(得分:0)
使用键对列表进行排序:
field['js'].sort(key=lambda k: k['title'])
有关详细信息,请参阅How do I sort a list of dictionaries by values of the dictionary in Python?
答案 1 :(得分:0)
一种方式:
>>> for data in sorted(field['js']):
... print data['title']
...
entertainments
information