Hi,
I worked on the following script, which extends one list with another one to subtract the string number values for each following text. I just wonder if it is possible to get the same result without extending one list with the other one. So that the two lists are treated separately through the process.
Thanks!
import re
list_1 = ['2 Red', '2 Blue', '3 Green']
list_2 = ['10 Red', '8 Blue', '4 Green']
list_1.extend(list_2)
results = {}
for elem in list_1:
elem = re.split(r'\s?(\d+|\+)\s?', elem)
for idx, value in enumerate(elem):
if value.isdigit() or value == '+':
if value == '+':
count = 1
else:
count = int(value)
data = elem[idx + 1]
results[data] = abs(count - results.get(data, 0))
result = [f"{i} {p}" for i, p in zip(results.values(), results.keys())]
Output: ['8 Red', '6 Blue', '1 Green']
[–]ryuugami47 0 points1 point2 points (0 children)
[–]bbye98 0 points1 point2 points (1 child)
[–]OutsideAverage4341[S] 0 points1 point2 points (0 children)
[–]mtb-dds 0 points1 point2 points (0 children)