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

陌路散爱 提交于 2019-12-13 01:45:51

问题


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?


回答1:


To match your expected output, you cannot use set.intersection as sets are unordered so if you get a as the first item it is totally by chance , you should make key a set and use in,iterating over the list returning the first match which will keep the order:

def findexact(lst):
    key = {'a','g','t'}
    for ele in lst:
        if ele in key:
            return ele
    return False

If you want to get all the matches and see the non matches just make key a set and use a loop:

def findexact(lst):
    key = {'a','g','t'}
    for ele in lst:
        if ele in key:
            print(ele)
        else:
            # do whatever

If you want to return a bool based on whether there are any common element use set.isdisjoint:

def findexact(lst):
    key = {'a','g','t'}
    return not key.isdisjoint(lst)

If there is at least one match, the function will return True, if not then the sets are disjoint so it will return False.

If you want the index use enumerate:

def findexact(lst):
    key = {'a','g','t'}
    for ind,ele in enumerate(lst):
        if ele in key:
            return ind, ele
    return False

That will return both the element and the index if we have a match, if you just want the index just return ind, for no match we simply return False




回答2:


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



回答3:


You don't need to do all that indexing.

def findexact(lst):
    key = ['a','g','t']
    for item in (set(key) & set(lst)):
        return item



回答4:


The simplest way is to use Python's built-in set intersection:

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

for item in set(lst).intersection(key):
    print item

Output

a

Putting that into a function that returns the exact matches:

def findexact(lst):
    key = ['a','g','t']
    return set(lst).intersect(key)

Or into a function that returns True if there is at least one exact match:

def findexact(lst):
    key = ['a','g','t']
    return bool(set(lst).intersect(key))



回答5:


lst = ['a', 'b', 'c', 'aa', 'bb', 'cc']
def findexact(lst):
    i=0
    key = ['a','g','t']
    for eachItm in lst:
        if eachItm in key:
            print eachItm

findexact(lst)

This should do what you trying




回答6:


This:

item in lst[i] for item in key

looks for each element of the key inside of each element of the list. And finds 'a' inside of 'a' and inside of 'aa'. It doesn't find 'g' or 't' inside of any element of the lst.



来源:https://stackoverflow.com/questions/32061078/python-returning-the-value-if-there-is-an-exact-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!