Question

I have a dictionary of Counters as such:

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

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:

9
2
3
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top