How do you perform operations between multiple dictionaries?

+2 Justin Red · November 16, 2014
I am very new to python and programming in general so please bare with me.

I don't have any code because I'm not sure how to even approach this problem...

Given a set of data where each entry has 4 values (str, str, float, float), the 1st string can only have 4 possibilities so I am guessing I should set each of those possibilities as a dictionary. Therefore, each dictionary would have a key and 2 values. The keys are all unique (will never repeat) and each dictionary can have different amount of keys.

Sample data...


Dict1 = {‘A’: [1.1, 100.1], ‘B’: [2.2, 200.2], ‘C’: [3.3, 300.3]}
Dict2 = {‘D’: [4.4, 400.4], ‘E’: [5.5, 500.5], ‘F’: [6.6, 600.6], ‘G’:[7.7, 700.7]}
Dict3 = {‘H’: [8.8, 800.8], ‘I’: [9.9, 900.9]}
Dict4 = {‘J’: [10.1, 100.1], ‘K’:[11.1, 101.1], ‘L’: [34.4, 345.3], ‘M’: [13.3, 123.3]}


I want to print out all possible combinations, using 1 key and both values, from each dictionary. I then want to be able to print the sum of the first value, and the 2nd value from the combination. So, as an example from the above set, a new dictionary could return the following:

Combo1 = {'A': [1.1, 100.1], 'D': [4.4, 400.4], 'H': [8.8, 800.8], 'J': [10.1, 100.1], 'Total1': [24.4, 1401.4]}

The formatting is irrelevant. Is this even possible? Use dictionaries? itertools?

Thanks for your help!

Post a Reply

Replies

Oldest  Newest  Rating
+1 Justin Red · November 19, 2014
I hate to "bump" this but I'm still at square 1 with this one. Can anyone point me in the right direction? Thanks!
+1 Doug Fresh · November 22, 2014
Well, I have a solution. I didn't use itertools or any modules. I'm 99% sure a module would make it way easier. First, I transferred the dictionary values to a list by first grabbing the keys from the Dicts, storing them in a list and iterating through the list positions to match up the keys in the dictionary. shown here:
Dict1 = {'A': [1.1, 100.1], 'B': [2.2, 200.2], 'C': [3.3, 300.3]}
listKeys = []
listValues1 = []

for i in sorted(Dict1):
    listKeys.append(i)
print(listKeys1)
for i in sorted(listKeys):
    listValues1.append(Dict1)
print(listValues1)
## once the dictionary is transferred, the listKeys will need to be cleared for
## the next Dictionary.
listKeys = []

Then I began the combination iterations. 
## if the above code is used to fill the list, then declaration will not be needed.
## simply begin @ comboList = []

list1 = [[1.1, 100.1],[2.2, 200.2],  [3.3, 300.3]]
list2 = [[4.4, 400.4], [5.5, 500.5], [6.6, 600.6], [7.7, 700.7]]
list3 = [[8.8, 800.8],[9.9, 900.9]]
list4 = [[10.1, 100.1], [11.1, 101.1], [34.4, 345.3], [13.3, 123.3]]

comboList = []
MIN_RANGE = 0

for firstSet in range(len(list1)):
    firstValue = list1[firstSet]
    
    for secondSet in range(len(list2)):
        secondValue = list2[secondSet]

        for thirdSet in range(len(list3)):
            thirdValue = list3[thirdSet]

            for fourthSet in range(len(list4)):
                fourthValue = list4[fourthSet]

                ## this handles the concatenation of each combo, and it's sum is displayed and rounded nicely.
                tempComboBuilder = [firstValue, secondValue, thirdValue, fourthValue,
                                    format(sum(firstValue) + sum(secondValue) + sum(thirdValue) + sum(fourthValue), ',.2f')]
                comboList.append(tempComboBuilder)
                comboList.append(' || ')

## this prints all the combos the sums of each combo
for possibleCombo in range(MIN_RANGE, len(comboList)):
    print(comboList[possibleCombo])

So, this is the start. I'm speculating that you want to be able to sort the combo lists again after all the combinations have been found. So you will have to figure out which Data Structure would suit you best for recalling or searching for specific information. 
Very interesting problem. Hopefully this helped!
+1 Justin Red · November 22, 2014
Thanks, Doug. Your solution seems to add up both values of one set from each list.

[[1.1, 100.1], [4.4, 400.4], [8.8, 800.8], [10.1, 100.1], '1,425.80']

How do you add up the first values and second values separately,to yield...

[[1.1, 100.1], [4.4, 400.4], [8.8, 800.8], [10.1, 100.1], '[24.4, 1401.4]'

Thanks again!
+2 Doug Fresh · November 22, 2014
No worries, sorry I missed that. The totals would need to be added separately. So this would replace the code that creates the comboList.

                ## this handles the concatenation of each combo, and it's sum is displayed and rounded nicely.
                tempTotal1 = round((firstValue[0] + secondValue[0] + thirdValue[0] + fourthValue[0]),1)
                tempTotal2 = round((firstValue[1] + secondValue[1] + thirdValue[1] + fourthValue[1]),1)
                
                tempTotals = [tempTotal1, tempTotal2]
                tempComboBuilder = [firstValue, secondValue, thirdValue, fourthValue, tempTotals]
                
                comboList.append(tempComboBuilder)
                comboList.append(' || ')

**Note, I made a change to the totals so they display properly, and their types will stay as floats, rather than a string. 
  • 1

Python

107,068 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator