Python: Incrementing int value in a dictionary -


so have 2 dictionaries each have key value pairs of following manner:

firstdict = {      'himjysulal': 0      'psnnxwfvmv': 0      '4b0ian1p5x': 0      'mxzzgolefq': 0 } 

what want achieve code following:

  1. loop on first , second dictionary. if value of first dictionary in second dictionary, increment int value of restaurant_key(in firstdict) 1

  2. do same, except main loop seconddict , inner loop firstdict(there's reason this). increment int value of restaurant_key(of seconddict) 1

the code runs , all, instead of getting want, 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 on , forth 

this not want. int values should vary. ideally should this:

(u'himjysulal', 4) (u'jxdxpoeuwy', 0) (u'ctnymkpcoe', 1) (u'vwsfnwtnz1', 5) (u'0zcfi67s6x', 2) 

here 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 merchants in initial spot k, v in firstdict.iteritems():     #for merchants in chosen spot     k2, v2 in seconddict.iteritems():              #if merchant appears in initial spot, , in chosen spot,              #end loop , go next one. we're interested in aren't in chosen spot.              #this means dropped.               if k == k2:                  print("broke with: " + k)                 break              else:                  #the merchant isn't in chosen spots,so therefore merchant dropped.                  firstdict[k] = firstdict[k] + 1  #for merchants in chosen spot k, v in seconddict.iteritems():     #for merchants in initial spot     k2, v2 in firstdict.iteritems():          #if merchant appears in chosen spot, in initial spot,         #end loop , go next merchant. means merchant         #originally selected.           if k == k2:              print("broke with: " + k)             break          else:              #the merchant isn't in initial spot, merchant added.              seconddict[k] = seconddict[k] + 1   obj in firstdict:     print(obj, firstdict[obj])  print(" ") print("chosen spots") print("++++++++++++") print(" ")  obj in seconddict:     print(obj, seconddict[obj]) 

thank in advance.

you don't need nested iterations stated operations:

for key in set(firstdict.keys()) & set(seconddict.keys()):     firstdict[key] += 1     seconddict[key] += 1 

the key notice both operations operate on keys dicts have in common, i.e. intersection. can use built-in set datatype, amazingly fast compared nested loops - not mention intention clearer :-)


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -