compare dictionary values

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

    #1

    compare dictionary values

    What's a good way to compare values in dictionaries? I want to find
    values that have changed. I look for new keys by doing this:

    new = [k for k in file_info_cur.i terkeys() if k not in
    file_info_old.i terkeys()]
    if new == []:
    print new, "No new files."
    else:
    print new, "New file(s)!!!"

    My key-values pairs are filepaths and their modify times. I want to
    identify files that have been updated or added since the script last ran.

    Thanks,
    rbt
  • Marc 'BlackJack' Rintsch

    #2
    Re: compare dictionary values

    In <dp4124$pki$1@s olaris.cc.vt.ed u>, rbt wrote:
    [color=blue]
    > What's a good way to compare values in dictionaries?[/color]

    Look them up and then compare!? ;-)
    [color=blue]
    > I want to find
    > values that have changed. I look for new keys by doing this:
    >
    > new = [k for k in file_info_cur.i terkeys() if k not in
    > file_info_old.i terkeys()]
    > if new == []:
    > print new, "No new files."
    > else:
    > print new, "New file(s)!!!"
    >
    > My key-values pairs are filepaths and their modify times. I want to
    > identify files that have been updated or added since the script last ran.[/color]

    This looks up each `key` from the `new` dictionary and compares the value
    with the `old` one. If it's not equal or the key is not present in `old`
    the key is appended to the `result`::

    def new_and_changed _keys(old, new):
    result = list()
    for (key, value) in new:
    try:
    if old[key] != value:
    result.append(k ey)
    except KeyError:
    result.append(k ey)
    return result

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • rbt

      #3
      Re: compare dictionary values

      Marc 'BlackJack' Rintsch wrote:[color=blue]
      > In <dp4124$pki$1@s olaris.cc.vt.ed u>, rbt wrote:
      >[color=green]
      >> What's a good way to compare values in dictionaries?[/color]
      >
      > Look them up and then compare!? ;-)
      >[color=green]
      >> I want to find
      >> values that have changed. I look for new keys by doing this:
      >>
      >> new = [k for k in file_info_cur.i terkeys() if k not in
      >> file_info_old.i terkeys()]
      >> if new == []:
      >> print new, "No new files."
      >> else:
      >> print new, "New file(s)!!!"
      >>
      >> My key-values pairs are filepaths and their modify times. I want to
      >> identify files that have been updated or added since the script last ran.[/color]
      >
      > This looks up each `key` from the `new` dictionary and compares the value
      > with the `old` one. If it's not equal or the key is not present in `old`
      > the key is appended to the `result`::
      >
      > def new_and_changed _keys(old, new):
      > result = list()
      > for (key, value) in new:
      > try:
      > if old[key] != value:
      > result.append(k ey)
      > except KeyError:
      > result.append(k ey)
      > return result
      >
      > Ciao,
      > Marc 'BlackJack' Rintsch[/color]

      Thanks Marc! I changed this line:

      for (key, value) in new:

      To this:

      for (key, value) in new.iteritems() :

      And, it works great. Thanks again.

      Comment

      Working...