Comparing value in two dictionaries?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gilles Ganault

    Comparing value in two dictionaries?

    Hello

    I fill two dictionaries with the same number of keys, and then need to
    compare the value for each key, eg.

    #Pour chaque APE, comparaison societe.ape.nom bre et verif.ape.nombr e
    import apsw

    #============
    dic1={}
    [...]
    rows=list(curso r.execute(sql))
    for id in rows:
    dic1[id[0]] = id[1]
    #============
    dic2={}
    [...]
    rows=list(curso r.execute(sql))
    for id in rows:
    dic2[id[0]] = id[1]
    #============
    #Here, compare each key/value to find values that differ
    for i in dic1.items():
    [...]

    What would be a good way to do this in Python?

    Thank you.
  • bearophileHUGS@lycos.com

    #2
    Re: Comparing value in two dictionaries?

    Gilles Ganault:
    I fill two dictionaries with the same number of keys, and then need to
    compare the value for each key, eg.
    Probably using the DBMS capabilities you can find a better solution.

    Are the keys equal? If you want to do it using dicts, you can iterate
    on one dict, with iteritems, and then using the key look for the value
    of the second dict, to see if they are the same, to collect
    differences, etc.

    Bye,
    bearophile

    Comment

    • Arnaud Delobelle

      #3
      Re: Comparing value in two dictionaries?

      Gilles Ganault <nospam@nospam. comwrites:
      Hello
      >
      I fill two dictionaries with the same number of keys, and then need to
      compare the value for each key, eg.
      >
      #Pour chaque APE, comparaison societe.ape.nom bre et verif.ape.nombr e
      import apsw
      >
      #============
      dic1={}
      [...]
      rows=list(curso r.execute(sql))
      for id in rows:
      dic1[id[0]] = id[1]
      #============
      dic2={}
      [...]
      rows=list(curso r.execute(sql))
      for id in rows:
      dic2[id[0]] = id[1]
      #============
      #Here, compare each key/value to find values that differ
      for i in dic1.items():
      [...]
      >
      What would be a good way to do this in Python?
      >
      Thank you.
      I think you would be better off writing some SQL query that does it.

      If you want to do this in Python, I suppose it depends whether you know
      that the two dictionaries have the same set of keys. If they do you can
      simply write something like:

      diff = [key for key, val1 in dic1.iteritems( ) if val1 != dic2[key]]

      --
      Arnaud

      Comment

      • Scott David Daniels

        #4
        Re: Comparing value in two dictionaries?

        Arnaud Delobelle wrote:
        Gilles Ganault <nospam@nospam. comwrites:
        >
        >Hello
        >>
        >I fill two dictionaries with the same number of keys, and then need to
        >compare the value for each key, ...
        if you know set(dic1) == set(dic2) -- that is that the same keys are
        used, you could use:
        Setup:
        >>dic1 = dict((c, ord(c)) for c in 'abcdefgh')
        >>dic2 = dic1.copy()
        >>dic2['e'] = 12
        >>dic2['h'] = 13
        Comparisons:
        >>differs = dict((p1[0], (p1[1], p2[1]))
        for p1, p2 in zip(sorted(dic1 .items()),
        sorted(dic2.ite ms()))
        if p1 != p2)
        >>differs
        {'h': (104, 13), 'e': (101, 12)}


        --Scott David Daniels
        Scott.Daniels@A cm.Org

        Comment

        • Arnaud Delobelle

          #5
          Re: Comparing value in two dictionaries?

          Arnaud Delobelle <arnodel@google mail.comwrites:
          Or to obtain a dictionary of differences:
          >
          dict((k, (v, dic2[v]) for k, v in dic1.iteritems( )
          if dic2[v] != v)
          ^
          Should be k of course!

          --
          Arnaud

          Comment

          Working...