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. This allows you to provide a 0 or empty dictionary if there is no value for the query.
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]
:
>>> 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
. If you give dict.get
a second argument, it will return that value if the key is missing.