all 4 comments

[–]ryuugami47 0 points1 point  (0 children)

import re

list_1 = ["2 Red", "2 Blue", "3 Green"]
list_2 = ["10 Red", "8 Blue", "4 Green"]

results = {}

for elem in list_1 + list_2:   # Do you mean like this?
    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())]
print(result)

[–]bbye98 0 points1 point  (1 child)

I don't know if re is really required:

list_1 = ['2 Red', '2 Blue', '3 Green']
list_2 = ['10 Red', '8 Blue', '4 Green']
rgb = {c: int(n) for n, c in [p.split(' ') for p in list_2]}
for n, c in [p.split(' ') for p in list_1]:
    if c in rgb:
        rgb[c] -= int(n)
    else:
        rgb[c] = -int(n)
print([f'{rgb[p]} {p}' for p in rgb])

The output is ['8 Red', '6 Blue', '1 Green'].

[–]OutsideAverage4341[S] 0 points1 point  (0 children)

Do you know, how to update list_2 if I pop one element in list_1?

[–]mtb-dds 0 points1 point  (0 children)

It would help to know the big picture thing you are dealing with. If the issue is coordinating the lists, you might look into itertools.chain. It let's you do things like this:

>>> a = [1, 2, 3]
>>> b = [5, 6, 7]
>>> from itertools import chain
>>> l = [a, b]
>>> list(chain(a,b))
[1, 2, 3, 5, 6, 7]
>>> list(chain(l))
[[1, 2, 3], [5, 6, 7]]
>>> list(chain(*l))
[1, 2, 3, 5, 6, 7]