Home > Enterprise >  Calculate the difference between two list, and store the result in a third list. Python
Calculate the difference between two list, and store the result in a third list. Python

Time:11-22

How would I calculate the difference between two separate list and store them in a third list.

for example...

list_1 [('M', 4000.0), ('R', 5320.0)]
list_2 [('M', 4222.0), ('R', 5442.0)]

I tried the following

list_3 = []

list_3.append([list_1] - [list_2])

print(list_3)

but I'm met with, a TypeError

TypeError: unsupported operand type(s) for -: 'list' and 'list'

CodePudding user response:

This seems like something better suited to a dictionary

dict_1 = {'M': 4000.0, 'R': 5320.0}
dict_2 = {'M': 4222.0, 'R': 5442.0}

dict_3 = {}
dict_3['M'] = dict_1['M'] - dict_2['M']
dict_3['R'] = dict_1['R'] - dict_2['R']

If you're set on using a list of tuples you could do something with

tuple(map(operator.add, tup1, tup2)

which allows you to add and subtract tuples. You would have to do something about the string values though

CodePudding user response:

OK, here's one possibility, which might or might not be what you want. It assumes that the tuples are in the same order in both lists and does not verify that the first elements of corresponding tuples are the same (which is possibly a bug in waiting). It produces the result of subtracting the numeric values in the second list from the numeric values in the first list, rather than the absolute value of the differences; in your example, both numbers are negative. (If you wanted the absolute value, use abs(v1 - v2))

It uses zip to combine the two lists, and a list comprehension to put the computed results back together into a list.

>>> def diff(list_1, list_2):
...     return [(first, v1 - v2) for ((first, v1), (_, v2)) in zip(list_1, list_2)]
... 
>>> 
>>> list_1 = [('M', 4000.0), ('R', 5320.0)]
>>> list_2 = [('M', 4222.0), ('R', 5442.0)]
>>> diff(list_1, list_2)
[('M', -222.0), ('R', -122.0)]

Comprehensions are good when you need to do an element-by-element computation from one or more input lists. The comprehension just states how to create each output element from the inputs; the possibly-unfamiliar syntax in the for clause states how to decompose the inputs into individual pieces for use by the computation.

zip takes two or more lists and constructs an iterator of tuples; the tuples take one value from each list and the iterator continues until one of the lists is exhausted. So if list1 is a list of Xs and list2 is a list of Ys, zip(list1, list2) produces an iterator over tuples of (X, Y). In this case, X and Y are both tuples, with the same structure: a letter and a number. So zip creates tuples of the form ((letter, number), (letter, number)) and the for clause takes that apart using the pattern ((first, v1), (_, v2)), which precisely matches the structure of the zipped tuples. (It must match or Python would complain.)

I use the variable name _ to indicate that the value is ignored (that's a convention, not a rule, but later you'll find that it is a rule in some Python constructs). The letter from the second list is ignored because there's nothing in the problem description which indicates what to do with it.

  • Related