Find matching strings in two lists python

Given two lists, the task is to write a Python program to extract all the strings which are possible substring to any of strings in another list.

Example:

Input : test_list1 = [“Geeksforgeeks”, “best”, “for”, “geeks”], test_list2 = [“Geeks”, “win”, “or”, “learn”]

Output : [‘Geeks’, ‘or’]

Explanation : “Geeks” occurs in “Geeksforgeeks string as substring.

Input : test_list1 = [“geeksforgeeks”, “best”, “4”, “geeks”], test_list2 = [“Geeks”, “win”, “or”, “learn”]

Output : []

Explanation : No substrings found.

Method #1: Using list comprehension

In this, we perform task of using nested loop and testing using list comprehension, extracting the string if its part of any substring of other list.

Python3

test_list1 = ["Geeksforgeeks", "best", "for", "geeks"]

test_list2 = ["Geeks", "win", "or", "learn"]

print["The original list 1 is : " + str[test_list1]]

print["The original list 2 is : " + str[test_list2]]

res = list[

    set[[ele1 for sub1 in test_list1 for ele1 in test_list2 if ele1 in sub1]]]

print["Substrings Intersections : " + str[res]]

Output

The original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks']
The original list 2 is : ['Geeks', 'win', 'or', 'learn']
Substrings Intersections : ['or', 'Geeks']

Method #2: Using any[] + generator expression

In this, any[] is used to check for substring matching in any of the strings from the string list to be matched in.

Python3

test_list1 = ["Geeksforgeeks", "best", "for", "geeks"]

test_list2 = ["Geeks", "win", "or", "learn"]

print["The original list 1 is : " + str[test_list1]]

print["The original list 2 is : " + str[test_list2]]

res = [ele2 for ele2 in test_list2 if any[ele2 in ele1 for ele1 in test_list1]]

print["Substrings Intersections : " + str[res]]

Output

The original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks']
The original list 2 is : ['Geeks', 'win', 'or', 'learn']
Substrings Intersections : ['Geeks', 'or']

Time Complexity: O[n2]

Space Complexity: O[n]

Method #3: Using find[] method

Python3

test_list1 = ["Geeksforgeeks", "best", "for", "geeks"]

test_list2 = ["Geeks", "win", "or", "learn"]

print["The original list 1 is : " + str[test_list1]]

print["The original list 2 is : " + str[test_list2]]

res=[]

for i in test_list2:

    for j in test_list1:

        if[j.find[i]!=-1 and i not in res]:

            res.append[i]

print["Substrings Intersections : " + str[res]]

Output

The original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks']
The original list 2 is : ['Geeks', 'win', 'or', 'learn']
Substrings Intersections : ['Geeks', 'or']


How do you find a match between two lists in Python?

We can club the Python sort[] method with the == operator to compare two lists. Python sort[] method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

How do I check if two strings match in Python?

Python strings equality can be checked using == operator or __eq__[] function. Python strings are case sensitive, so these equality check methods are also case sensitive.

How do you compare lists with strings?

The similarity between Lists and Strings in Python is that both are sequences. The differences between them are that firstly, Lists are mutable but Strings are immutable. Secondly, elements of a list can be of different types whereas a String only contains characters that are all of String type.

How do you match a string to a list in Python?

Python Find String in List using count[] We can also use count[] function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1.

Bài mới nhất

Chủ Đề