473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Associating tuple values to a dictionary key and subsequent values

GTXY20
29
Have a little bit of a problem:

I have the following dictionaries:

Expand|Select|Wrap|Line Numbers
  1. d={'1': ['a', 'b', 'c', 'd'], '3': ['a', 'b', 'c', 'd'], '2': ['a', 'b', 'c', 'd'], '4': ['a', 'c']}
  2. update={'a': 10, 'c': 4, 'b': 5, 'd': 1}
I use the following to output:


Expand|Select|Wrap|Line Numbers
  1. Combqtyoutfile = open('Combqty.txt', 'w')
  2.     Combqtyoutfile.write('Qty\tNumber\tCombination\n')
  3.     from collections import defaultdict
  4.     fcomb=defaultdict(int)
  5.     for v in d.values():
  6.         fcomb[tuple(v)]+=1
  7.     for count, items in sorted( ((v,k) for k,v in fcomb.items()),reverse=True):
  8.         comb=','.join(items)
  9.         hnum = len(items)
  10.         Combqtyoutfile.write ('%s\t%s\t%s\n' % (count,hnum,comb))
  11.     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.
Oct 5 '07 #1
1 1642
bvdet
2,851 Expert Mod 2GB
Have a little bit of a problem:

I have the following dictionaries:

Expand|Select|Wrap|Line Numbers
  1. d={'1': ['a', 'b', 'c', 'd'], '3': ['a', 'b', 'c', 'd'], '2': ['a', 'b', 'c', 'd'], '4': ['a', 'c']}
  2. update={'a': 10, 'c': 4, 'b': 5, 'd': 1}
I use the following to output:


Expand|Select|Wrap|Line Numbers
  1. Combqtyoutfile = open('Combqty.txt', 'w')
  2.     Combqtyoutfile.write('Qty\tNumber\tCombination\n')
  3.     from collections import defaultdict
  4.     fcomb=defaultdict(int)
  5.     for v in d.values():
  6.         fcomb[tuple(v)]+=1
  7.     for count, items in sorted( ((v,k) for k,v in fcomb.items()),reverse=True):
  8.         comb=','.join(items)
  9.         hnum = len(items)
  10.         Combqtyoutfile.write ('%s\t%s\t%s\n' % (count,hnum,comb))
  11.     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.
Expand|Select|Wrap|Line Numbers
  1. >>> update={'a': 10, 'c': 4, 'b': 5, 'd': 1}
  2. >>> d={'1': ['a', 'b', 'c', 'd'], '3': ['a', 'b', 'c', 'd'], '2': ['a', 'b', 'c', 'd'], '4': ['a', 'c']}
  3. >>> for key in d:
  4. ...     summation = sum([update[s] for s in d[key]])
  5. ...     print summation
  6. ...     
  7. 20
  8. 20
  9. 20
  10. 14
  11. >>> 
Oct 7 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Thomas Guettler | last post by:
Hi! Can someone point me to the documentation of tuples __hash__ method? I want to unique a list of tuples. Since __hash__ returns a 32 bit integer, there could be a situtation where two...
3
by: Jinming Xu | last post by:
Sorry for the previous message. It's really a simple question and I have solved it myself. Thanks, Jinming ------------------------------------------------------------------------ Hi Folks,
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
17
by: Pierre Fortin | last post by:
Hi, Is the following a reasonable generic approach for converting python returned tuples/lists into dicts...? I'm not advocating library functions also return dicts (I'd probably spend more...
12
by: Kay Schluehr | last post by:
Hi all, thanks for Your attention ! I think my proposal was more in mind of Rons modified exec than Pythons lambda. When George proposed his unpacking behavoir for list-comps as a pack of...
9
by: Odd-R. | last post by:
I have a dictionary, and I want to convert it to a tuple, that is, I want each key - value pair in the dictionary to be a tuple in a tuple. If this is the dictionary {1:'one',2:'two',3:'three'},...
4
by: laxmikiran.bachu | last post by:
Can we have change a unicode string Type object to a Tuple type object.. If so how ????
11
by: montyphyton | last post by:
Recently, I got into a debate on programming.reddit.com about what should happen in the following case: Currently, Python raises an error *and* changes the first element of the tuple. Now,...
3
by: bruce | last post by:
hi guys/gals... got a basic question that i can't get my hands around. i'm trying to programatically create/use a list/tuple (or whatever the right phrase in pyton is!!) basically,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.