从python中的字典获取值

2024-06-28 11:16:22 发布

您现在位置: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}

如果我试图通过以下方式获得值:

你知道吗索引.get('(V,NP)')

它什么也不回。如何检索值?你知道吗

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


Tags: theindexnpclausenompp词典det
3条回答

你想要的是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

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

相关问题 更多 >