Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Returning the value if there is an "exact" match?

lst = ['a', 'b', 'c', 'aa', 'bb', 'cc']

def findexact(lst):
    i=0
    key = ['a','g','t']
    while i < len(lst):
        if any(item in lst[i] for item in key):
            print lst[i]
        i+=1

findexact(lst)

in the above code, the result comes out to be:

'a'
'aa'

I would like the result to be:

'a'

What is the right way to use any() to get the right result?

like image 335
niamleeson Avatar asked Apr 19 '26 12:04

niamleeson


1 Answers

Based on my interpretation of your question, it looks like you want to find which item in key is in lst. This would be the way to do it:

def findexact(lst):
    key = ['a','g','t']
    for k in key:
        if k in lst:
            print k
            return k
like image 117
Nayuki Avatar answered Apr 22 '26 03:04

Nayuki