Python 如果给定值,如何打印字典中的键

Python 如果给定值,如何打印字典中的键,python,dictionary,Python,Dictionary,这是我的字典,里面有单词和定义: Vocab={'Precaution' : "a measure taken in advance to avert possible evil or to secure good results", 'Cautious' : "showing, using, or characterized by caution", 'Cautionary' : "of the nature of or containing a warning", 'Dissuade' :

这是我的字典,里面有单词和定义:

Vocab={'Precaution' : "a measure taken in advance to avert possible evil or to secure good results",
'Cautious' : "showing, using, or characterized by caution",
'Cautionary' : "of the nature of or containing a warning",
'Dissuade' : "to deter by advice or persuasion; persuade not to do something",
'Persuasion' : "the act of persuading or seeking to persuade"}
这是另一本字典,但是,这本字典包含拉丁词根作为键,vocab作为值

 Roots={'Caut' :{'Precaution', 'Catious', 'Cautionary'}, 'Saud' :{'Dissuade', 'Persuasion'}}
这是一个小测验游戏:

print "If you want to know the root of the word, type 'root'"
while 1:
    y = random.choice(Vocab.keys())
    print y
    t2=raw_input("What is the definition?: ")
    if t2 in Vocab[y]:
        print 'All those words were in the definition!'
        print Vocab[y]
    elif t2 not in Vocab[y]:
        if t2 == 'root':
            print Roots
        elif t2 != 'root':
            for key,y in Roots.iteritems()):
                print key

我希望用户输入“root”,然后弹出root作为提示。当词根弹出后,屏幕上将出现相同的问题词,供他尝试回答。当用户输入“root”时,整个字典就会出现。如何让它打印出单词所在的词根?

我不确定我是否理解您的需要,但为什么不将词根与主要词汇直接存储在一个数据结构中?这将使打印当前单词的词根变得容易,而不需要任何反向字典或其他查找策略

vocab = {
    'Precaution' : {'root': 'Caut', 'def': 'definition': 'a measure ...'},
    'Cautious'   : {'root': 'Caut', 'def': 'showing, using, ...'},
    'Dissuade'   : {'root': 'Saud', 'def': 'to deter ...'},
}
这种方法也与O-O设计相一致,如果您的问题将来需要朝这个方向发展,每个
单词
实例都将包含其相关属性:定义、根、可选拼写等

例如:

wroots = { w : r for r, ws in Roots.iteritems() for w in ws }
vocab  = { w : dict(root = wroots[w], defin = d) for w, d in Vocab.iteritems() }

我不确定我是否理解您需要什么,但为什么不将词根与主要词汇直接存储在一个数据结构中呢?这将使打印当前单词的词根变得容易,而不需要任何反向字典或其他查找策略

vocab = {
    'Precaution' : {'root': 'Caut', 'def': 'definition': 'a measure ...'},
    'Cautious'   : {'root': 'Caut', 'def': 'showing, using, ...'},
    'Dissuade'   : {'root': 'Saud', 'def': 'to deter ...'},
}
这种方法也与O-O设计相一致,如果您的问题将来需要朝这个方向发展,每个
单词
实例都将包含其相关属性:定义、根、可选拼写等

例如:

wroots = { w : r for r, ws in Roots.iteritems() for w in ws }
vocab  = { w : dict(root = wroots[w], defin = d) for w, d in Vocab.iteritems() }

我不确定我是否理解您需要什么,但为什么不将词根与主要词汇直接存储在一个数据结构中呢?这将使打印当前单词的词根变得容易,而不需要任何反向字典或其他查找策略

vocab = {
    'Precaution' : {'root': 'Caut', 'def': 'definition': 'a measure ...'},
    'Cautious'   : {'root': 'Caut', 'def': 'showing, using, ...'},
    'Dissuade'   : {'root': 'Saud', 'def': 'to deter ...'},
}
这种方法也与O-O设计相一致,如果您的问题将来需要朝这个方向发展,每个
单词
实例都将包含其相关属性:定义、根、可选拼写等

例如:

wroots = { w : r for r, ws in Roots.iteritems() for w in ws }
vocab  = { w : dict(root = wroots[w], defin = d) for w, d in Vocab.iteritems() }

我不确定我是否理解您需要什么,但为什么不将词根与主要词汇直接存储在一个数据结构中呢?这将使打印当前单词的词根变得容易,而不需要任何反向字典或其他查找策略

vocab = {
    'Precaution' : {'root': 'Caut', 'def': 'definition': 'a measure ...'},
    'Cautious'   : {'root': 'Caut', 'def': 'showing, using, ...'},
    'Dissuade'   : {'root': 'Saud', 'def': 'to deter ...'},
}
这种方法也与O-O设计相一致,如果您的问题将来需要朝这个方向发展,每个
单词
实例都将包含其相关属性:定义、根、可选拼写等

例如:

wroots = { w : r for r, ws in Roots.iteritems() for w in ws }
vocab  = { w : dict(root = wroots[w], defin = d) for w, d in Vocab.iteritems() }

建议1:找到输入字在值中的键值对,然后打印键值

if t2 == 'root':
    for root,words in Roots.iteritems():
        if y in words:
            print root
            break
建议2:创建此词典

invRoots = {word:root for root,words in Roots.iteritems() for word in words}
和使用

if t2 == 'root':
    print invRoots[y]

另一件事:你的
词根
包含一个输入错误:
'Catious'
建议1:找到值中输入单词的键值对,然后打印键值

if t2 == 'root':
    for root,words in Roots.iteritems():
        if y in words:
            print root
            break
建议2:创建此词典

invRoots = {word:root for root,words in Roots.iteritems() for word in words}
和使用

if t2 == 'root':
    print invRoots[y]

另一件事:你的
词根
包含一个输入错误:
'Catious'
建议1:找到值中输入单词的键值对,然后打印键值

if t2 == 'root':
    for root,words in Roots.iteritems():
        if y in words:
            print root
            break
建议2:创建此词典

invRoots = {word:root for root,words in Roots.iteritems() for word in words}
和使用

if t2 == 'root':
    print invRoots[y]

另一件事:你的
词根
包含一个输入错误:
'Catious'
建议1:找到值中输入单词的键值对,然后打印键值

if t2 == 'root':
    for root,words in Roots.iteritems():
        if y in words:
            print root
            break
建议2:创建此词典

invRoots = {word:root for root,words in Roots.iteritems() for word in words}
和使用

if t2 == 'root':
    print invRoots[y]


另一件事:你的
包含一个打字错误:
'Catious'

你试过什么?你有什么问题?您是否考虑过创建一个反向字典以使查找更容易?@jonsrsharpe我希望在用户输入“root”时显示随机选择的值的键。是的,我理解;我的问题是“你自己为实现这一目标付出了什么努力?”@jonsrsharpe我从昨天开始就在寻找答案。我什么也找不到。我尝试过其他方法,我知道我可以创建一个反向字典。我想知道是否有较短的路。这就是我为什么问这个问题的原因。那么,也许这更适合于;只需使用反向字典编写您的工作代码,纠正其他错误,然后将其发布到那里,看看是否有人可以提出任何改进建议。您尝试了什么?你有什么问题?您是否考虑过创建一个反向字典以使查找更容易?@jonsrsharpe我希望在用户输入“root”时显示随机选择的值的键。是的,我理解;我的问题是“你自己为实现这一目标付出了什么努力?”@jonsrsharpe我从昨天开始就在寻找答案。我什么也找不到。我尝试过其他方法,我知道我可以创建一个反向字典。我想知道是否有较短的路。这就是我为什么问这个问题的原因。那么,也许这更适合于;只需使用反向字典编写您的工作代码,纠正其他错误,然后将其发布到那里,看看是否有人可以提出任何改进建议。您尝试了什么?你有什么问题?您是否考虑过创建一个反向字典以使查找更容易?@jonsrsharpe我希望在用户输入“root”时显示随机选择的值的键。是的,我理解;我的问题是“你自己为实现这一目标付出了什么努力?”@jonsrsharpe我从昨天开始就在寻找答案。我什么也找不到。我尝试过其他方法,我知道我可以创建一个反向字典。我想知道是否有较短的路。这就是我为什么问这个问题的原因。那么,也许这更适合于;把你的工作写下来