1

I have a list of short strings, and a list of multiple long strings. I would like to iterate through the short strings list and find which of the short strings is in all the items of a certain list.
for example:

short_str_list = ["aga", "ttt", "aca"]

seq_list = ["atcgcgtacat", "acatcgggattt", "tttacagtgtgtggg"]

the result should be: "aca"

Can someone help me with the python script for it?

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
Bella
  • 937
  • 1
  • 13
  • 25
  • Does this answer your question? [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – sahasrara62 May 04 '20 at 17:00

2 Answers2

0

The following double loop expression does it:

print([ss 
       for ss in short_str_list
       if all(ss in ls for ls in seq_list)
       ])
Dov Grobgeld
  • 4,783
  • 1
  • 25
  • 36
0

Break down the problem into pieces.

Start off by making a function that checks if one string is in a sequence of strings:

def check_one(string, seqs):
    for seq in seqs:
        if string not in seq:
            return False
    return True

Then make a function that checks all strings by using this function:

def check_all(strings, seq):
    result = []

    for string in strings:
        if check_one(string, seq):
            result.append(string)

    return result

Then simply call check_all():

short_str_list = ["aga", "ttt", "aca"]
seq_list = ["atcgcgtacat", "acatcgggattt", "tttacagtgtgtggg"]

print(check_all(short_str_list, seq_list))
# ['aca']

You can also use all() , filter() and list comprehensions to to do this in one line:

print([x for x in short_str_list if all(x in y for y in seq_list)])
# ['aca']

print(list(filter(lambda x: all(x in y for y in seq_list), short_str_list)))
# ['aca']
RoadRunner
  • 25,803
  • 6
  • 42
  • 75