简体   繁体   中英

How to compare list and return match and non match in python?

how to compare the 2 list of same length and return the matches and non matches elements have to append as a single element with space

l1=["a","b","c"]
l2=["a","d","c"]

result=[]
for i in l1:
    for j in l2:
        if i == j:
            match = i
            result.append(match)
        else:
            non_match = i + " "+ j
            result.append(non_match)

print(result)

Actual Output:
['a', 'a d', 'a c', 'b a', 'b d', 'b c', 'c a', 'c d', 'c']

Expected Output:
["a","b d","c"]

As long as order of the items in the output doesn't matter, you could do this:

Output = list(map(lambda x: " ".join(set(x)), zip(List1, List2)))

>>> Output
['a', 'd b', 'c']

The logic can be broken down at follows:

1: zip the two lists together:

# display the zipped lists:
>>> list(zip(List1, List2))
[('a', 'a'), ('b', 'd'), ('c', 'c')]

2: Turn each tuple in the resulting list into a set (to get unique values):

# display the result of calling set on the zipped lists
>>> list(map(set, zip(List1, List2)))
[{'a'}, {'d', 'b'}, {'c'}]

3: concatenate the members of each set with join

Output = list(map(lambda x: " ".join(set(x)), zip(List1, List2)))

Loop through both lists, appending a space & the List2 element if the corresponding elements are not equal to each other.

[List1[i] + (f" {List2[i]}" if List1[i] != List2[i] else '') for i in range(len(List1))]

I'll add @grind's answer as well for completeness. I think we both like it a bit better. As mentioned it doesn't need indexes and the formatting also includes the concatenation of left & right which I consider an improvement too.

[left if left == right else f'{left} {right'} for left, right in zip(List1, List2)]

The first one will throw an IndexError if the lengths of the two lists are different. The second will result in a new list which has a length equal to the shorter of the two input lists.

Other answers tell other methods, but I solve your issue. The issue with you code is because you are running through the python list completely two times. You can use zip function from python here. I have solved your code for you.

l1=["a","b","c"]
l2=["a","d","c"]

result=[]
for i,j in zip(l1, l2):
    if i == j:
        match = i
        result.append(match)
    else:
        non_match = i + " "+ j
        result.append(non_match)

print(result)

Try:

[' '.join((a, b)) if a != b else a
 for a, b in zip(l1, l2)]

zip(l1, l2) can let you iterate l1, l2 simultaneously, ''.join((a, b)) if a != b else a is a Conditional Expression which expresses what you want. Conditional Expression part can be evaluated to value, which will eventually aggregate into the result you want through the list comprehension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM