在python中通过键访问元组

时间:2015-02-28 05:48:31

标签: python hashmap

我有概率哈希映射,其中键是概率值,值是它们的类型。为了获得最高概率,我对哈希映射进行了排序,但我无法弄清楚如何访问具有最高键值的值。

probability = {classify_email(mailmessage, android_term_df):"android" ,
                           classify_email(mailmessage, spam_term_df):"spam" ,
                           classify_email(mailmessage, c_term_df):"c" ,
                           classify_email(mailmessage, cplus_term_df):"cplus",
                           classify_email(mailmessage, java_term_df):"java",
                           classify_email(mailmessage, php_term_df):"php" ,
                           classify_email(mailmessage, easyham_term_df):"ham" 
                           }
            sorted_probability = sorted(probability.items(), key=operator.itemgetter(0))

2 个答案:

答案 0 :(得分:0)

你甚至不需要排序:

highest_probabilty_type = probability[max(probability.keys())]

答案 1 :(得分:0)

如果你的两个类具有完全相同的概率,那么只有其中一个类将保留在这样的代码中的字典中。

您应该使用类作为键概率 作为价值

from operator import itemgetter

dfs = {
    "android": android_term_df,
    "spam": spam_term_df,
    "c": c_term_df,
    "cplus": cplus_term_df,
    "java": java_term_df,
    "php": php_term_df,
    "ham": ham_term_df,
}

probabilities = { cls: classify_email(mailmessage, dfs[cls]) for cls in dfs }

# now they are classes -> probability
max_cls, max_prob = max(probabilities.items(), key=itemgetter(1))

或者,如果您需要排序的分发列表,请将它们放入(probability, class)元组列表中:

probabilities = [ (classify_email(mailmessage, dfs[cls], cls)) for cls in dfs ]
probabilities.sort()

请注意,与原始示例不同,在重复的情况下,您不会丢失任何对。