Home > OS >  Comparing Lists and Removing Them
Comparing Lists and Removing Them

Time:06-18

I have this list of lists:

[
    ["Alpha", 100],
    ["Alpha", 200],
    ["Alpha", "A1"],
    ["Alpha", "A2"],
    [100, "A1"],
    [200, "A2"],
    ["Alpha", 100, "A1"],
    ["Alpha", 200, "A2"],
]

I'm looking for correlations and relationships in this list of lists.

My goal is to compare each array and remove those that conflict. What I mean is, ['ALPHA', 100] and ['ALPHA', 200] would be removed because they are "not true." (ALPHA = 100, ALPHA = 200, not true). IF ALPHA is seen with both values, that means that it doesn't correlate with either of them... it is not a pattern. So in this list, just by looking at it, I can tell this is my desired outcome:

[[100, A1], [200, 'A2'] [ALPHA, 100, 'A1'] [ALPHA, 100, 'A2']]

because:

Alpha = 100 (conflict -> remove)

Alpha = 200 (conflict -> remove)

Alpha = 'A1' (conflict -> remove)

Alpha = 'A2' (conflict -> remove)

100 = 'A1' (keep)

200 = 'A2' (keep)

Alpha, 100 = A1 (keep)

Alpha, 200 = A2 (keep)

Is there code like this that already exists? and if clarification is needed I can edit the question.

CodePudding user response:

It is not very clear from your description how you want to remove the elements, but based on your example - it looks like you are trying to remove all "inner lists" where the first n-1 elements have multiple mappings to the nth (last) element -

One way to do that is -

from collections import Counter
keys = [' '.join(str(_) for _ in k) for *k, _ in l]
# ['Alpha', 'Alpha', 'Alpha', 'Alpha', '100', '200', 'Alpha 100', 'Alpha 200']
c = Counter(keys)
# Counter({'Alpha': 4, '100': 1, '200': 1, 'Alpha 100': 1, 'Alpha 200': 1})
required_elements = [k   [_] for *k, _ in l if c[' '.join(str(_) for _ in k)] == 1]  
# [[100, 'A1'], [200, 'A2'], ['Alpha', 100, 'A1'], ['Alpha', 200, 'A2']]

CodePudding user response:

Reading between the lines, perhaps this fits your need (where lst contains your list of lists):

from collections import Counter

cnt = Counter([tuple(k) for *k, v in lst])
out = [k   [v] for *k, v in lst if cnt[tuple(k)] == 1]

>>> out
[[100, 'A1'], [200, 'A2'], ['Alpha', 100, 'A1'], ['Alpha', 200, 'A2']]
  • Related