Home > Net >  Search elements in a list in another list and check each element exist or not
Search elements in a list in another list and check each element exist or not

Time:07-14

I have 2 lists as

x = ["abc", "def", "ghi"]

y = ["ggg", "hhh", "abc", "yyy", "ttt", "uuu", "ooo". "def", "www", "xxx"]

I am trying to create a new lists called match

when there is a match of values of list x in y the match list should get appended if not present then it should be appended with Not Found.

Like in the above case the match list should be

match = ["Present", "Present", "Not Found"]

I tried different logics but am not getting the right answer, below is what I tried

match = []
for j in range(len(x)):
        for k in range(len(y)):
            if x[j]==y[k]:
                match.append("Present")
            else:
                pass
        match.append("Not Found")

Another way

or j in range(len(x)):
        for k in range(len(y)):
            if x[j]==y[k]:
                match.append("Present")
            else:
                match.append("Not found")

Another way I tried is

for k in range(len(x)):
        for j in range(len(y)):
            if x[k]==y[j]:
                match.append("Present")

  
    for k in range(len(x)):
        for j in range(len(y)):
            if x[k]==y[j]:
                pass
            else:
                match.append("Not Found")

I believe the logic is simple but I ma not getting around it. Please help!

CodePudding user response:

Update add memory for x to search each x one-time to get better run-time.

mem_x = {i: 'Not Found' for i in set(x)}
set_y = set(y)
for k,v in mem_x.items():
    if k in set_y:
        mem_x[k] = 'Present'
        
match = [mem_x[i] for i in x]
print(match)

OLD

You can use set(y) for getting better performance in searching and then search each value of x exists in set(y) or not.

x = ["abc", "def", "ghi"]

y = ["ggg", "hhh", "abc", "yyy", "ttt", "uuu", "ooo", "def", "www", "xxx"]

set_y = set(y)

match = ['Present' if i in set_y else 'Not Fount' for i in x]
print(match)

Output:

['Present', 'Present', 'Not Fount']

Or create an empty list and append in it. (But the second approach without list.append() is a better approach.)

match = []
set_y = set(y)
for i in x:
    if i in set_y:
        match.append('Present')
    else:
        match.append('Not Fount')

CodePudding user response:

I'll consider only your second try:

for j in range(len(x)):
        for k in range(len(y)):
            if x[j]==y[k]:
                match.append("Present")
            else:
                match.append("Not found")

By putting match.append("Not found") in the else branch then"Not found" will be appended to match for each item y[k] that is different than x[j]. For example for "abc" you will insert to match successively, "Not found", "Not found", "Present", "Not found" and so on.

The thing is that you can only conclude that the item is Not found after the inner loop terminates. Then you will to check if "Present" has been put in your list. For this you can use a boolean variable initialised to False and set to True if the item is present. This gives:

for j in range(len(x)):
        found = False
        for k in range(len(y)):
            if x[j]==y[k]:
                match.append("Present")
                found = True
                break
        if not found:
            match.append("Not found")

The break statement is there to avoid looping on remaining items if the x[j] has been found in y and also to avoid putting "Present" several times in the list if x[j] appears several times in y.

CodePudding user response:

As @I'mahdi said, function set() can solve your problem perfectly. Actually there are many bulit-in functions, try to be familiar with them.

As to your impletation:

  1. This will always add a "Not Found" to match list, you should continue when match completes; len(match) = len(x) len(present)
match = []
for j in range(len(x)):
        for k in range(len(y)):
            if x[j]==y[k]:
                match.append("Present")
            else:
                pass
        match.append("Not Found")

2.This will lead to too many "Not found", because every unmatch will be recorded.(len(match) = len(x) * len(y))

or j in range(len(x)):
        for k in range(len(y)):
            if x[j]==y[k]:
                match.append("Present")
            else:
                match.append("Not found")

3.The same as 2.

    for k in range(len(x)):
        for j in range(len(y)):
            if x[k]==y[j]:
                match.append("Present")

  
    for k in range(len(x)):
        for j in range(len(y)):
            if x[k]==y[j]:
                pass
            else:
                match.append("Not Found")
  • Related