Have a little bit of a problem:
I have the following dictionaries:
I use the following to output:
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 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}
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()
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.
Comment