2014-02-09 37 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']