Home > front end >  list in list, search in an another list
list in list, search in an another list

Time:12-14

I have a first list, containing string lists and a second list containing strings. I want that when a string from the first list is found in the second, the string from the second is returned and the search resumes at the next string from the first list. It's hard to explain...

list1 = [
    ['test', 'string'],
    ['hello', 'world']
    ['good', 'morning','everyone']]

list2 = [
    ['test is here'],
    ['hi everyone'],
    ['world of code']]

if list1[0][0] is in list2[0], then return list2[0] and go to list1[1] (don't want to test list1[0][1]).

I've tried nesting for loops inside other for loops with break and if conditions but can't get the solution.

I hope someone will understand the problem and find a solution.

CodePudding user response:

If the length of lists is the same, the code should be something like this:

list1 = [
    ['test', 'string'],
    ['hello', 'world'],
    ['good', 'morning', 'everyone']]

list2 = [
    ['test is here'],
    ['hi everyone'],
    ['world of code']]

answer = []

for l1 in range(len(list1)):
    for item in list1[l1]:
        if item in str(list2[l1]):
            answer.append(item)
            break
print(*answer)

and output will be

test

Hope this helped! :D

CodePudding user response:

First, here are some tips for solving problems like this in the future:

First, the format of your input data is pretty strange (in my opinion), so I'd really recommend trying to flatten everything out -- this may or may not be possible depending on the larger structure of your program, so if you can't actually do that, then no big deal. Something like:

list1 = [ ['test', 'string'], ['hello', 'world'], ['good', 'morning', 'everyone'] ]
list2 = ['test is here', 'hi everyone', 'world of code']

is already easier to work with from a data processing standpoint, and that's just from flattening list2.

Second, conceptually, you have to have two loops going on here -- the first is to iterate through list1 (so if you find a match in list2, you can return the match and move onto the next member of list1), and another to go through the individual elements of list1, since a string in list2 could match on any one of the strings from the inner lists in list1 (i.e. a string in list2 could be matched either by 'test' or 'string').

With that said, here's a (pretty inefficient) solution to your problem:

def check_long_str(long_strings, check_list):

    for check in check_list: #iterate over individual strings in each inner list of list1
        for long_string_item in long_strings: #look for a match in list2
            long_string = long_string_item[0] #weird format
            if check in long_string:
                print('"{}" found "{}"'.format(check, long_string))
                return long_string

    return None

def check_for_strings(search_strings, long_strings):

    toReturn = set()
    for search_list in search_strings: #iterate over inner lists of list1
        found_str = check_long_str(long_strings, search_list)
        if found_str is not None:
            toReturn.add(found_str)

    return toReturn

print(check_for_strings(list1, list2))

Output:

"test" found "test is here"
"world" found "world of code"
"everyone" found "hi everyone"
set(['test is here', 'hi everyone', 'world of code'])

CodePudding user response:

So there was an error in the code you gave. I found an issue in the code you gave. There is a typo which is a missing "," after one of the lists. This is right after the:

    ['hello', 'world']

list in list1.

Although there may be better ways to loop through a nested list, this is how I would do it.

list1 = [
    ['test', 'string'],
    ['hello', 'world'],
    ['good', 'morning', 'everyone']]

list2 = [
    ['test is here'],
    ['hi everyone'],
    ['world of code']]
found_words = []
for i in range(len(list1)):
    for x in range(len(list1[i])):
        for z in list2:
            for y in z:
                teststring = y.split()
                for j in teststring:
                    if j in list1[i][x]:
                        found_words.append(j)
                        break
print(found_words)

Output:

['test', 'world', 'everyone']

Hopefully this helps!

  • Related