Have a little bit of a problem:
I have the following dictionaries: - 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: - 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.
1 1642 bvdet 2,851
Expert Mod 2GB
Have a little bit of a problem:
I have the following dictionaries: - 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: - 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. - >>> 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
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,
|
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,...
|
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...
|
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...
|
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'},...
|
by: laxmikiran.bachu |
last post by:
Can we have change a unicode string Type object to a Tuple type
object.. If so how ????
|
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,...
|
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,...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |