Python 我如何浏览这样的词典?

Python 我如何浏览这样的词典?,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我找到了以下词典: catalog = {"threads":{"39894014":{"date":1390842451,"r":0,"i":0,"lr": {"id":39894014},"semantic_url":"the-g-wiki-g-is-for-the-discussion- of-technology","sticky":1,"closed":1,"capcode":"mod"}} 我知道我可以通过写数据[“threads”][“39894014”][“date”]来访

我找到了以下词典:

catalog = {"threads":{"39894014":{"date":1390842451,"r":0,"i":0,"lr":  {"id":39894014},"semantic_url":"the-g-wiki-g-is-for-the-discussion-  of-technology","sticky":1,"closed":1,"capcode":"mod"}}

我知道我可以通过写
数据[“threads”][“39894014”][“date”]
来访问“date”,但是有没有办法跳过第二件事(
[“39894014”]
),因为这个数字是随机生成的,所以列表可能会更改,而且它将是一个完全不同的数字

您必须循环使用
'threads'
键的字典值的键或值;如果字典中只有一个线程,可以使用以下方法提取该字典:

thread = next(data['threads'].values())
print(thread['date'])
如果不需要外部词典保持原始状态,可以使用展开:

threadid, thread = data['threads'].popitem()
print(thread['date'])
要获取所有条目,您可以循环并获取日期:

all_dates = [thread['data'] for thread in data['threads'].values()]

这不是Python语法
var
在我看来很像JavaScript。是的,因为它是,所以我尝试通过json访问它(忽略var目录),如果该字典中的
threads
键下几乎没有条目,你如何判断你需要哪一个呢?那么为什么要包含
var
?只需关注在Python中访问嵌套字典的问题,不管您如何加载它。我会尝试获取所有条目。TypeError:“dict_values”对象不支持索引我做错了什么?@IThelp:mea culpa,Python 2 vs.Python 3让我大吃一惊。更正。非常感谢,问题解决了^^为什么不使用
next(data['threads'].values())
@ThiefMaster:是的,一个更好的主意。