计算字典中的值 [英] Counting values in dictionary

查看:74
本文介绍了计算字典中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下字典.

dictA = { 
    'a' : ('duck','duck','goose'), 
    'b' : ('goose','goose'), 
    'c' : ('duck','duck','duck'), 
    'd' : ('goose'), 
    'e' : ('duck','duck') 
    }

我希望遍历dictA并输出一个列表,该列表将向我显示dictA中具有多个鸭子"值的键.

I'm hoping to loop through dictA and output a list that will show me the keys in dictA that have more than one "duck" in value.

例如,对于dictA,此函数将输出以下列表.

For example, for dictA this function would output the below list.

list = ['a', 'c', 'e']

我敢肯定有一个简单的方法可以做到这一点,但是我是Python的新手,这让我很困惑.

I'm sure there is an easy way to do this, but I'm new to Python and this has me stumped.

推荐答案

[k for (k, v) in dictA.iteritems() if v.count('duck') > 1]

这篇关于计算字典中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆