I have the following code, which works fine for adding string elements from 2 lists:
list_1 = ['2 Red', '8 Blue', '4 Green']
list_2 = ['10 Red', '2 Blue', '3 Green']
list_1.extend(list_2)
results = {}
for elem in list_1:
number, color = elem.split()
results[color] = results.get(color, 0) + int(number)
result = [f"{i} {p}" for i, p in zip(results.values(), results.keys())]
Output: ['12 Red', '10 Blue', '7 Green']
Now, I want to do basic subtraction for the same elements, that the output is as follows:
Output: ['8 Red', '6 Blue', '1 Green']
I thought I understood my code, but obviously I don't, I get stuck with the + operator for int(numbers) & I do not understand the zip() function. I hope you guys can help me.
Stay healthy and have a nice day!
question from:
https://stackoverflow.com/questions/65918866/subtract-string-list-elements-from-2-lists 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…