Home > front end >  Getting elements from a list that contain any string from two other lists in python
Getting elements from a list that contain any string from two other lists in python

Time:09-09

Basically searching items from a list that contains combined elements from two other lists. Also, all the items in the three lists are individual strings.

Example:

Paths = ['ab01', 'bc02', 'cd03', 'de04', 'ef05']
str1 = ['b','e']
str2 = ['3', '4']

# desired output
out = ['de04']       # only item in Paths that contains both elements from str1 and str2

CodePudding user response:

You can use list-comprehension with any():

out = [
    p for p in Paths if any(s in p for s in str1) and any(s in p for s in str2)
]
print(out)

Prints:

['de04']

CodePudding user response:

Basic solution:

>>> Paths = ['ab01', 'bc02', 'cd03', 'de04', 'ef05']
>>> str1 = ['b','e']
>>> str2 = ['3', '4']
>>> out = []
>>> for i in range(len(Paths)):
...     for j in range(len(str1)):
...         if str1[j] in Paths[i]:
...              for k in range(len(str2)):
...                  if str2[k] in Paths[i]:
...                      out.append(Paths[i])
...
>>> out
['de04']

CodePudding user response:

this solution could help you achieve your goal, it will work with any number of paths and strings, Best of luck !

Paths = ['ab01', 'bc02', 'cd03', 'de04', 'ef05']
str1 = ['b','e']
str2 = ['3', '4']

out = ['de04']

def program(paths, *lists):
    out = []
    for path in paths:
        bools = [any([x in path for x in l]) for l in lists]
        if all(bools):
            out.append(path)
    return out


if __name__ == "__main__":
    out = program(Paths, str1, str2)
    print(out)

CodePudding user response:

You can use list comprehension for this, but it's a bit complicated.

First you can iterate through each value in Paths, as i:

[i for i in Paths...]

But then you need to check if a value from str1 or str2 exists in i, these can be done with more list comprehension as below (not these lines will not work on their own unless you define i to test:

[j for j in str1 if j in i]
[k for k in str2 if k in i]

The above will return a list of the characters from each str that exist in i. A useful thing in python is that an empty list will automatically evaluate to False, and a populated one will evaluate to True, so you just need to put these comprehensions into the first one, after an if statement:

out =  [i for i in Paths if [j for j in str1 if j in i] and [k for k in str2 if k in i]]
print(out)

['de04']

Although the below replaces i,j and k to be a bit more readable:

[path for path in Paths if [elem1 for elem1 in str1 if elem1 in path] and [elem2 for elem2 in str2 if elem2 in path]]

This code basically does the same as below, but in a single line. I have included the below to help with understanding:

out = []
for i in Paths:
    list1 = []
    for j in str1:
        if j in i:
            list1.append(j)

    list2 = []
    for k in str2:
        if k in i:
            list2.append(k)

    if list1 and list2:
        out.append(i)
  • Related