繁体   English   中英

从python中的字典中获取值

[英]Get value from a dictionary in python

我有以下字典:

index=  {(V, NP): VP, ('the',): Det, ('shot',): V, (V, NP, Det, Nom): VP, (Det, Nom): NP, ('Harry',): PropN, (N,): Nom, (P, VP): PP, ('dog',): N, (V,): VP, (V, S): VP, ('thinks',): V, ('Mary',): PropN, ('eager',): Adj, (NP, VP, Conj, Clause): Clause, ('gave',): V, (NP, VP): RelClause, ('give',): V, ('to',): P, (V, Adj, PP): VP, ('is',): V, ('cat',): N, (Clause,): S, ('easy',): Adj, (Adj, Nom): Nom, ('my',): Det, ('flower',): N, (V, NP, PP): VP, ('i',): PropN, (V, NP, Whw, RelClause): VP, (V, Adj): VP, ('policeman',): N, ('please',): V, ('a',): Det, ('pajamas',): N, ('This',): PropN, ('very',): Adj, ('John',): PropN, ('that',): Whw, ('said',): V, (PropN,): NP, ('pis',): V, ('chased',): V, ('apple',): N, (P, NP): PP, ('I',): PropN, ('elephant',): N, (VP, NP): RelClause, ('book',): N, ('an',): Det, ('heavy',): Adj, ('teacher',): N, ('persuaded',): V}

如果我尝试通过以下方式获取价值:

index.get('(V, NP)')

它什么都不返回。 如何检索值?

ps:我尝试了几乎所有的组合来从字典中得到一些东西。

你想要index.get((V, NP)) ,而不是index.get('(V, NP)')

您应该尝试index.get(('V', 'NP'))index.get((V, NP)) (取决于元组的项目是字符串还是变量)而不是index.get('(V, NP)')

看起来您的密钥是变量元组而不是字符串。 您必须使用相同的键来检索值,例如

>>>index = {(1,2):3}

>>>index['(1,2)']# this will fail
KeyError: '(1,2)'

>>>index[(1,2)]
3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM