简体   繁体   English

如何从符合特定条件的列表中返回对象-Python

[英]How to return objects from a list that matches certain criterias - Python

I'm Writing a search function where I'm searching through a list for matching Words or characters. 我正在编写一个搜索功能,在其中搜索要匹配的单词或字符的列表。 It looks like 看起来像

    def search(seq, db):
        return match(db, seq)

Where match is a recursive function which searches through the lists and returns a boolean value. 其中match是一个递归函数,它搜索列表并返回布尔值。 How do I return a list with the matching objects instead of just True or False? 如何返回带有匹配对象的列表,而不仅仅是True或False?

The match functions looks like this: 匹配函数如下所示:

def match(seq, pattern):

    if not pattern:
        return not seq

    elif pattern[0] == '--':

        if match(seq, pattern[1:]):
            return True

        elif not seq:
            return False

        else:
            return match(seq[1:], pattern)

    elif not seq: 
        return False 

    elif pattern[0] == '&':
        return match(seq[1:], pattern[1:])

    elif seq[0] == pattern[0]:
        return match(seq[1:], pattern[1:])

    elif isinstance(seq[0], list):
        return match(seq[0], pattern)

    elif isinstance(pattern[0], list):
        return match(seq, pattern[0])

    else:
        return False

I don't really understand what your match function is doing. 我不太了解您的match功能在做什么。 I'm guessing you're wanting the match function to return a list instead of a boolean value. 我猜想您要让match函数返回一个列表,而不是布尔值。

You can start by changing your base cases. 您可以从更改基本案例开始。 Everywhere you see return False or return True instead say 'return [] or return [seq[0]] (assuming seq[0]` is a sensible thing to be returned in your list of matches). 您看到的所有地方都return Falsereturn True而不是说'return [] or return [seq [0]] (assuming seq [0]`是在匹配列表中返回的明智之举)。

With your return match(...) recursive cases you could return [seq[0]] + match(...) or something similar to add the current match (when applicable) to the listed returned by the recursive call. 对于您的return match(...)递归案例,您可以return [seq[0]] + match(...)或类似的东西,以将当前匹配项(如果适用)添加到递归调用返回的列表中。

This method of prepending lists is not particularly efficient, but I doubt that your concern now. 这种将列表放在前面的方法并不是特别有效,但是我怀疑您现在的担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用标准在Python中列出生成器 - List generator in Python with criterias 如果满足某些条件,如何让我的 output 返回 True 或 False? - How can I make my output return True or False if the certain criterias are met? 返回带有正则表达式python列表的列表中的匹配项 - return matches in a list with list of regex python 如何从 API 返回带有特定参数的列表? - How to return a list with a certain parameter from an API? 如何接收对象属性的任意组合并从 Python 中的对象列表中返回匹配的对象? - How to receive any combination of an object's attributes and return the matching objects from a list of objects in Python? 如何在python中的列表列表中比较匹配数并返回不匹配项 - How to compare number of matches in list of lists in python and return non-matches 如何从日期时间列表中减去n分钟并返回匹配的日期时间 - How to subtract n minutes from list of datetime and return the datetime that matches 如何仅从满足 python 某些条件的列表中的 select 对象 - How to only select objects from a list which satisfy certain conditions in python python 从列表中查找匹配项 - python find matches from a list Python:返回从列表中过滤出的对象的 index() 列表 - Python: return a list of the index() of objects filtered from a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM