Restructure dictionary (Python 2.5)

Discussion in 'Python' started by Fencer, Mar 2, 2009.

  1. Fencer

    Fencer Guest

    Hello, I have a dictionary that has strings as keys and for each key the
    associated value is a list of strings. Many of the lists contain only
    one element and a given element may appear for more than one key.
    What I need to do now is create a new dictionary where the strings in
    the list are keys and their values is a list of which keys in the "old"
    dictionary that contained them.
    So I want to go from:
    aa:[a, b, c]
    bb:[c, d]
    to
    a:[aa]
    b:[aa]
    c[aa, bb]
    d:[bb]

    I tried this code:
    old_dict = {'xyz':['a','b','c'],'baz':['c','d']}
    new_dict = {}
    for dkey, vallist in old_dict.iteritems():
    for val in vallist:
    if val in new_dict:
    theset = new_dict[val]
    theset.add(dkey)
    new_dict[val] = theset
    else:
    new_dict[val] = set([dkey])
    print new_dict

    Which yields the output {'a': set(['xyz']), 'c': set(['xyz', 'baz']),
    'b': set(['xyz']), 'd': set(['baz'])}, so it seems to work but I have a
    feeling this is a crappy solution that's underusing Python. Please
    enlighten me of a better way!

    - Fencer
    Fencer, Mar 2, 2009
    #1
    1. Advertising

  2. Fencer

    Tim Chase Guest

    > I tried this code:
    > old_dict = {'xyz':['a','b','c'],'baz':['c','d']}
    > new_dict = {}
    > for dkey, vallist in old_dict.iteritems():
    > for val in vallist:
    > if val in new_dict:
    > theset = new_dict[val]
    > theset.add(dkey)
    > new_dict[val] = theset
    > else:
    > new_dict[val] = set([dkey])
    > print new_dict
    >
    > Which yields the output {'a': set(['xyz']), 'c': set(['xyz', 'baz']),
    > 'b': set(['xyz']), 'd': set(['baz'])}, so it seems to work but I have a
    > feeling this is a crappy solution that's underusing Python. Please
    > enlighten me of a better way!


    In python2.5, collections.defaultdict was added which could clean
    this up a bit:

    import collections
    # using a set to remove duplication
    new_dict = collections.defaultdict(set)
    for dkey, vallist in old_dict.iteritems():
    for val in vallist:
    new_dict[val].add(dkey)

    # using a list to keep all keys
    new_dict = collections.defaultdict(list)
    for dkey, vallist in old_dict.iteritems():
    for val in vallist:
    new_dict[val].append(dkey)

    If you're working in pre-2.5 python, you could use

    new_dict = {}
    ...
    # use a set()
    new_dict.setdefault(val, set()).add(dkey)
    # use a list()
    new_dict.setdefault(val, []).append(dkey)

    -tkc
    Tim Chase, Mar 2, 2009
    #2
    1. Advertising

  3. Fencer

    imageguy Guest

    > ... this is a crappy solution that's underusing Python. Please
    > enlighten me of a better way!
    >
    > - Fencer


    One thing that springs to mind is using the dict method .setdefault

    <untested>
    for dkey, vallist in old_dict.iteritems():
    for val in vallist:
    new_dict.setdefault(val, set()).add(dkey)
    </untested>
    imageguy, Mar 2, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    427
    Ilias Lazaridis
    Feb 21, 2006
  2. Gerard Flanagan

    From the Tutorial Wiki: suggested restructure

    Gerard Flanagan, Apr 30, 2006, in forum: Python
    Replies:
    0
    Views:
    247
    Gerard Flanagan
    Apr 30, 2006
  3. tropos
    Replies:
    6
    Views:
    430
    Gavin Deane
    Sep 21, 2006
  4. james_027
    Replies:
    1
    Views:
    307
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  5. Rajat Tandon

    "Restructure a Windows application" from scratch in 2 months

    Rajat Tandon, Jan 20, 2005, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    171
    Ajay Kalra
    Jan 20, 2005
Loading...

Share This Page