检查键是否已经在字典中尝试除了

时间:2016-06-26 18:37:47

标签: python

我正在使用字典来计算不同项目在数据集中出现的次数。在类的 init 中,我将属性创建为像这样的字典

self.number_found = {}

我第一次找到任何特定项目时,如果我尝试这样做,我会得到一个KeyError,因为该项目尚未出现在字典中

self.number_found[item] = 1

所以我最终创建了一个函数来检查条目是否已经在字典中,如果没有,则首次添加

 def _count_occurrences(self, item):

    try:
        #this checks to see if the item's already in the dict
        self.number_found[item] = self.number_found[item] + 1
        x = self.number_found[item] 
    except KeyError:
        x = 1
        #this adds an item if not in the dict
        self.number_found[item] = x
        return x

但是,如果我在数据集中发现第二次出现项目,则无法正常工作。

假设我的数据集中有两个“大象”。当我将self.number_found打印到控制台时,这就是我得到的

{'elephant': 1}
{'elephant': None}

我在添加第二次出现时收到此错误

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

问题:检查密钥是否已经在字典中的正确方法是什么(解释为什么1正在变为None

2 个答案:

答案 0 :(得分:4)

您可以使用defaultdict

from collections import defaultdict

self.number_found = defaultdict(int)

首次访问某个项目时,其值将采用默认值0

返回None,因为您> <{1}}分支

答案 1 :(得分:2)

必须移出except块末尾的返回值。这样,两种情况都返回x

class C(object):
     def __init__(self):
        self.number_found = {}

     def _count_occurrences(self, item):
        try:
            #this checks to see if the item's already in the dict
            self.number_found[item] = self.number_found[item] + 1
            x = self.number_found[item] 
        except KeyError:
            x = 1
            #this adds an item if not in the dict
            self.number_found[item] = x
        return x

c = C()

r = c._count_occurrences('elephant')
print r
print c.number_found
r = c._count_occurrences('elephant')
print r
print c.number_found

这是一个测试运行,首先是缩进返回,然后是它在OP中的位置:

jcg@jcg:~/code/python/stack_overflow$ python number_found.py
1
{'elephant': 1}
2
{'elephant': 2}
jcg@jcg:~/code/python/stack_overflow$ python number_found.py
1
{'elephant': 1}
None
{'elephant': 2}

如您所见,第二个版本返回None,因为_count_occurrences尝试块没有返回