Python: Incrementing int value in a dictionary

So I have two dictionaries that each have key value pairs of the following manner:

firstDict = {
     'HImJYsulal': 0
     'psNnxwFVmv': 0
     '4B0IaN1P5x': 0
     'MxZzGOlefq': 0
}

What I want to achieve with my code is the following:

  1. Loop over the first and second dictionary. If the value of the first dictionary is in the second dictionary, increment the int value of the restaurant_key(in firstDict) by 1

  2. Do the same, except the main for loop is secondDict and the inner loop is firstDict(there's a reason for this). Increment int value of the restaurant_key(of secondDict) by 1

The code runs and all, but instead of getting what I want, I am getting:

(u'HImJYsulal', 0)
(u'jXDXpoeuWY', 1)
(u'ctNyMKpCoE', 2)
(u'vWsFNwTnz1', 3)
(u'0zcfI67S6X', 4)
(u'aGjUr2Ittw', 5)
(u'eQ5rGvpoRs', 6)
(u'6Q96oO26ua', 7)
...and so on and so forth

This is not what I want. the int values should vary. Ideally it should look something like this:

(u'HImJYsulal', 4)
(u'jXDXpoeuWY', 0)
(u'ctNyMKpCoE', 1)
(u'vWsFNwTnz1', 5)
(u'0zcfI67S6X', 2)

Here is the code:

import read_spins

firstDict = {}

# initialSpots
    read_spins.read_json('initialSpots.json', firstDict)

#for obj in firstDict:
    #print(obj,firstDict[obj])


#chosenSpots

secondDict = {}

read_spins.read_json('chosenSpots.json', secondDict)


#for all merchants in the initial spot
for k, v in firstDict.iteritems():
    #for all merchants in the chosen spot
    for k2, v2 in secondDict.iteritems():

            #if the merchant appears in the initial spot, and also in the chosen spot, 
            #end the loop and go to the next one. We're only interested in those that aren't in the chosen spot. 
            #This means that they were dropped. 

            if k == k2:

                print("broke with: " + k)
                break

            else:

                #the merchant isn't in the chosen spots,so therefore the merchant was dropped. 
                firstDict[k] = firstDict[k] + 1

#for all merchants in the chosen spot
for k, v in secondDict.iteritems():
    #for all merchants in the initial spot
    for k2, v2 in firstDict.iteritems():

        #if the merchant appears in the chosen spot, but also in the initial spot,
        #end the loop and go to the next merchant. THis means the merchant was
        #originally selected. 

        if k == k2:

            print("broke with: " + k)
            break

        else:

            #the merchant isn't in the initial spot, thus the merchant was added. 
            secondDict[k] = secondDict[k] + 1


for obj in firstDict:
    print(obj, firstDict[obj])

print(" ")
print("CHOSEN SPOTS")
print("++++++++++++")
print(" ")

for obj in secondDict:
    print(obj, secondDict[obj])

Thank you very much in advance.

Upvotes: 1

Views: 402

Answers (2)

Vivek Sable
Vivek Sable

Reputation: 10213

Incrementing int value in a both dictionary by 1 if key present in each other

  1. Get common key from the both dictionary by set intersection method.
  2. Iterate over common keys.
  3. Increment value by 1 for common key for respective dictionary.

Demo:

>>> a = {"a": 1, "b":2, "c":0}
>>> b = {"d": 1, "b":2, "c":0}
>>> ab = set(b.keys()).intersection(set(a.keys()))
>>> ab
set(['c', 'b'])
>>> for i in ab:
...     a[i] = a[i] + 1
...     b[i] = b[i] + 1
... 
>>> a
{'a': 1, 'c': 1, 'b': 3}
>>> b
{'c': 1, 'b': 3, 'd': 1}

Issue with your code:

Need to increment only when k==k2. So every time code goes in else loop when this condition is false and in else we are incrementing value by 1.

Just increment value in if loop.

Try 1 :

for k, v in secondDict.iteritems():
    increment = False        
    for k2, v2 in firstDict.iteritems():
        if k == k2:
            secondDict[k] = secondDict[k] + 1
            print("broke with: " + k)
            break

Try 2:

for k, v in secondDict.iteritems():
    if k in firstDict:
        secondDict[k] = secondDict[k] + 1

Upvotes: 1

thebjorn
thebjorn

Reputation: 27311

You don't need to do nested iterations to do your stated operations:

for key in set(firstDict.keys()) & set(secondDict.keys()):
    firstDict[key] += 1
    secondDict[key] += 1

the key is to notice that both your operations operate on keys that the dicts have in common, i.e. the intersection. Then you can use the built-in set datatype, which will be amazingly fast compared to nested loops - not to mention that your intention will be clearer :-)

Upvotes: 1

Related Questions