简体   繁体   English

访问计数器字典中的值-Python?

[英]Accessing values in dictionary of counters - python?

I have a dictionary of Counters as such: 我有一个这样的Counters字典:

dictofcounters = {'a':{'a':4,'b':2,'c':0}, 
                  'b':{'a':3,'b':9}
                  'c':{'a':0,'b':0, 'c':4}}

And I want to find the value where: 我想在以下位置找到值:

  • key == value == query 键==值==查询
  • sum of value where value == query 价值总和,其中价值==查询
  • sum of value where key==query and key != value 值的总和,其中key == query和key!= value

I have tried the following but is there any other way to achieve the same value? 我尝试了以下方法,但是还有其他方法可以实现相同的价值吗?

dictofcounters = {'a':{'a':4,'b':2,'c':0}, 
                  'b':{'a':3,'b':9}
                  'c':{'a':0,'b':0, 'c':4}}

query = 'c'

# value where key == value == query.
print dictofcounters[query][query]

# sum of value where value == query.
print sum(dictofcounters[i][query] for i in dictofcounters)

# sum of value where key==query and key != value.
print sum(i for i in dictofcounters[query] if i != query)

The desired output should be: 所需的输出应为:

4
0
0

If query = 'b' , then the output should be: 如果query = 'b' ,则输出应为:

9
2
3

One thing you can do to improve your code is to use functions. 您可以改善代码的一件事是使用函数。 For example: 例如:

def getQueryQuery(dictofcounters, query):
    # value where key == value == query.
    return dictofcounters.get(query, {}).get(query, 0)

def getSumOfQueryFromAll(dictofcounters, query):
    # sum of value where value == query.
    return sum(dictofcounters[i].get(query, 0) for i in dictofcounters)

def getQueryExceptForQuery(dictofcounters, query):
    # sum of value where key==query and key != value.
    return sum(i for i in dictofcounters.get(query, {}) if i != query)

One other improvement here is that by using the get method of a dictionary, you can supply a default value. 此处的另一项改进是,通过使用字典的get方法,您可以提供默认值。 This allows you to provide a 0 or empty dictionary if there is no value for the query. 如果查询没有值,这使您可以提供0或空的字典。

You could also have another method that calls each of these three methods and prints or returns the result: 您还可以使用另一个方法来调用这三个方法中的每一个并打印或返回结果:

def getAllDesiredQueryValues(dictofcounters, query):
    if you_want_to_print:
        print getQueryQuery(dictofcounters, query)
        ...
    else:
        return (getQueryQuery(dictofcounters, query), getSumOfQueryFromAll(dictofcounters, query), getQueryExceptForQuery(dictofcounters, query))

More on why you may want to use dict.get(i) rather than dict[i] : 有关为什么您可能想使用dict.get(i)而不是dict[i]

>>> myDict = {'a':1}
>>> myDict['a']
1
>>> myDict.get('a')
1
>>> myDict.get('a', 'defaultvalue')
1
>>> myDict['b']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'b'
>>> myDict.get('b')
>>> myDict.get('b', 'defaultvalue')
'defaultvalue'

As you can see, if the dictionary has the key they do the same thing. 如您所见,如果字典具有键,它们将执行相同的操作。 However, if the dictionary is missing the key, dict[i] will raise an exception, while dict.get(i) will return None . 但是,如果字典缺少键,则dict[i]将引发异常,而dict.get(i)将返回None If you give dict.get a second argument, it will return that value if the key is missing. 如果给dict.get提供第二个参数,则如果缺少键,它将返回该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM