Associating tuple values to a dictionary key and subsequent values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GTXY20
    New Member
    • Oct 2007
    • 29

    Associating tuple values to a dictionary key and subsequent values

    Have a little bit of a problem:

    I have the following dictionaries:

    Code:
    d={'1': ['a', 'b', 'c', 'd'], '3': ['a', 'b', 'c', 'd'], '2': ['a', 'b', 'c', 'd'], '4': ['a', 'c']}
    update={'a': 10, 'c': 4, 'b': 5, 'd': 1}
    I use the following to output:


    Code:
    Combqtyoutfile = open('Combqty.txt', 'w')
        Combqtyoutfile.write('Qty\tNumber\tCombination\n')
        from collections import defaultdict
        fcomb=defaultdict(int)
        for v in d.values():
            fcomb[tuple(v)]+=1
        for count, items in sorted( ((v,k) for k,v in fcomb.items()),reverse=True):
            comb=','.join(items)
            hnum = len(items)
            Combqtyoutfile.write ('%s\t%s\t%s\n' % (count,hnum,comb))
        Combqtyoutfile.close()
    this outputs to my text file:

    QTY Number Combination
    3 4 a,b,c,d
    1 2 a,c

    What I want to do is also include sum of values associated with elements in the combination based on the dictionary 'update' so that my file would then read:

    QTY Number Combination Sum
    3 4 a,b,c,d 20
    1 2 a,c 14

    Just wondering how I can associate the update dictionary in the for loop where the combination counts are determined.

    Any advice is greatley appreciated.

    Thanks in advance.

    G.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by GTXY20
    Have a little bit of a problem:

    I have the following dictionaries:

    Code:
    d={'1': ['a', 'b', 'c', 'd'], '3': ['a', 'b', 'c', 'd'], '2': ['a', 'b', 'c', 'd'], '4': ['a', 'c']}
    update={'a': 10, 'c': 4, 'b': 5, 'd': 1}
    I use the following to output:


    Code:
    Combqtyoutfile = open('Combqty.txt', 'w')
        Combqtyoutfile.write('Qty\tNumber\tCombination\n')
        from collections import defaultdict
        fcomb=defaultdict(int)
        for v in d.values():
            fcomb[tuple(v)]+=1
        for count, items in sorted( ((v,k) for k,v in fcomb.items()),reverse=True):
            comb=','.join(items)
            hnum = len(items)
            Combqtyoutfile.write ('%s\t%s\t%s\n' % (count,hnum,comb))
        Combqtyoutfile.close()
    this outputs to my text file:

    QTY Number Combination
    3 4 a,b,c,d
    1 2 a,c

    What I want to do is also include sum of values associated with elements in the combination based on the dictionary 'update' so that my file would then read:

    QTY Number Combination Sum
    3 4 a,b,c,d 20
    1 2 a,c 14

    Just wondering how I can associate the update dictionary in the for loop where the combination counts are determined.

    Any advice is greatley appreciated.

    Thanks in advance.

    G.
    I did not understand your question at first.[code=Python]>>> update={'a': 10, 'c': 4, 'b': 5, 'd': 1}
    >>> d={'1': ['a', 'b', 'c', 'd'], '3': ['a', 'b', 'c', 'd'], '2': ['a', 'b', 'c', 'd'], '4': ['a', 'c']}
    >>> for key in d:
    ... summation = sum([update[s] for s in d[key]])
    ... print summation
    ...
    20
    20
    20
    14
    >>> [/code]

    Comment

    Working...