Home > Software design >  With list of tuples corresponding to int values, want to create a unique list of those tuples corres
With list of tuples corresponding to int values, want to create a unique list of those tuples corres

Time:11-19

I have a huge list of sublists, each sublist consisting of a tuple and an int. Example:

[[(1, 1), 46], [(1, 2), 25.0], [(1, 1), 25.0], [(1, 3), 19.5], [(1, 2), 19.5], [(1, 4), 4.5], [(1, 3), 4.5], [(1, 5), 17.5], [(1, 4), 17.5], [(1, 6), 9.5], [(1, 5), 9.5]]

I want to create a unique list of those tuples corresponding to the sum of all those integer values using python. For the example above, my desired output looks like this:

[[(1, 1), 71], [(1, 2), 44.5], [(1, 3), 24], [(1, 4), 22], [(1, 5), 27], [(1, 6), 9.5]]

Could I get some help on how to do this?

I have tried to use dictionaries to solve this problem, but I keep running into errors, as I am not too familiar with how to use them.

CodePudding user response:

You can try this (though there's probably a shorter way):

a= [[(1, 1), 46], [(1, 2), 25.0], [(1, 1), 25.0], [(1, 3), 19.5], [(1, 2), 19.5], [(1, 4), 4.5], [(1, 3), 4.5], [(1, 5), 17.5], [(1, 4), 17.5], [(1, 6), 9.5], [(1, 5), 9.5]]

b = {}

for l in a:
    if b.get(l[0]):
        b[l[0]]  = l[1]
    else:
        b[l[0]] = l[1]

c = [[x,y] for x,y in b.items()]

# [[(1, 1), 71.0],
#  [(1, 2), 44.5],
#  [(1, 3), 24.0],
#  [(1, 4), 22.0],
#  [(1, 5), 27.0],
#  [(1, 6), 9.5]]

CodePudding user response:

Using a dictionary to solve.

Since it's a list of lists, you will need to specify what you want the key in the dictionary to be. In this case you wan't it to be the tuple. So when looping through that'll be at index 0 e.g. item[0]. You set the number as the value item[1] to the tuple. So you add to dictionary, if it's already in dictionary you add the two number " =" if it's not in dictionary you just set the value "="

example = [[(1, 1), 46], [(1, 2), 25.0], [(1, 1), 25.0], [(1, 3), 19.5], [(1, 2), 19.5], [(1, 4), 4.5], [(1, 3), 4.5], [(1, 5), 17.5], [(1, 4), 17.5], [(1, 6), 9.5], [(1, 5), 9.5]]

dict_tuples = {}
# tuple will be key in dict
for item in example:
    #see if tuple item[0]  is in dict. 
    # if it's in dict already, add to it's value
    if item[0] in dict_tuples:
        dict_tuples[item[0]]  = item[1]
    # if it's not in dict 
    else:
        dict_tuples[item[0]] = item[1]

# if you want it back in a list
alist = list(dict_tuples.items())
print(alist)

CodePudding user response:

Solution

A method using defaultdict goes like this:

from collections import defaultdict

L = [[(1, 1), 46], [(1, 2), 25.0], [(1, 1), 25.0], [(1, 3), 19.5], [(1, 2), 19.5], [(1, 4), 4.5], [(1, 3), 4.5], [(1, 5), 17.5], [(1, 4), 17.5], [(1, 6), 9.5], [(1, 5), 9.5]]

out = defaultdict(lambda: 0)

for [key, val] in L:
    out[key]  = val

out = [[each] for each in out.items()]

Returns:

[[((1, 1), 71.0)], [((1, 2), 44.5)], [((1, 3), 24.0)], [((1, 4), 22.0)], [((1, 5), 27.0)], [((1, 6), 9.5)]]

Explanation:

out is created as a defaultdict, such that out[somekey] returns 0 if somekey is not already a key in out.

We then go through every [tuple-int] in your original list, and add the value of int to out[tuple]. If tuple isn't already a key in out, then tuple takes its place among out's keys and the value assigned to it becomes int.

Finally we loop through out to reshape it into the form you wanted - [[tuple, int]]. If this particular format's not too important to you, you could skip that last step and just convert out.items() to a list like this:

print (list(out.items()))    
# returns:
[((1, 1), 71.0), ((1, 2), 44.5), ((1, 3), 24.0), ((1, 4), 22.0), ((1, 5), 27.0), ((1, 6), 9.5)]
  • Related