2014-02-09 35 views
0

所以我有一個名爲'useraccounts'的字典。我設法通過做來獲得所有那些在12類字典項的鍵:使用列表中的項目作爲字典鍵

found = ([k for k in useraccounts if useraccounts[k]['class'] == '12']) 

返回的列表是:

['bob', 'terry'] 

這是正確的。無論如何,我可以將這些結果作爲關鍵字,然後單獨使用它們單獨打印字典中的相關信息。例如,得到程序打印結果:

useraccounts[THE_KEYS_FOUND_IN_THE_LIST]['totalscore'] 

任何幫助,非常感謝。

在此先感謝。

回答

1

我想你想要的東西,如:

for name in found: 
    print(name, useraccounts[name]['totalscore']) 
+0

非常感謝:D – MrPython

+1

http://stackoverflow.com/help/someone-answers – jonrsharpe

0

你可以建立在你有什麼之前:

scores = [useraccounts[k]['totalscore'] for k in useraccounts if useraccounts[k]['class'] == '12'] 
# scores = [ list of scores ] 
0

如果你只是想爲那些特定鍵totalscore:

totals = [(key,value['totalscore') for key, value in useraccounts.items() if value['class'] == '12']