如何遍历嵌套字典(python)? [英] how do i traverse nested dictionaries (python)?

查看:576
本文介绍了如何遍历嵌套字典(python)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,所以如果我听不懂,请原谅我!!

i'm incredibly new to python so please forgive me if i don't understand something!!

我有125行代码,但是我有一个问题部分.当前设置的单词拼写错误.它链接到字典中拼写相似的单词,并且单词的得分基于它们的相似程度.

I've got a 125 lines of code but I have one problem section. as it's currently set up, there is an incorrectly spelled word. it links to similarly spelled words in the dictionary, and the words have a score based off of how similar they are.

possible_replacements("sineaster", {"sineaster":{"easter":0.75, "sinister":0.60}})

possible_replacements是函数的名称,"sineaster"是拼写错误的单词,而"easter"和&建议使用险恶"替代品.我想访问与字典单词相关的数字(.75和.6),但由于它们嵌套在另一本字典中,因此我似乎无法到达它们.

possible_replacements is the name of the function, "sineaster" is the misspelled word, and "easter" & "sinister" are recommended substitutes. I want to access the correlated numbers to the dictionary words (the .75 and .6), but I can't seem to reach them because they're nested within another dictionary.

有什么建议吗?

推荐答案

一旦您知道要查询哪个单词(在这里为"sineaster"),您就只需一个简单的

Once you know which word to query (here 'sineaster'), you just have a simple dictionary that you can, for example, traverse in a for loop:

outer_dict = {"sineaster":{"easter":0.75, "sinister":0.60}}
inner_dict = outer_dict["sineaster"]
for key, value in inner_dict.items():
    print('{}: {}'.format(key, value))

这篇关于如何遍历嵌套字典(python)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆