如何优化这段 python 代码?我需要改进它的运行时间

How to optimize this python code? I need to improve its runtime

我想优化这个过滤功能。它在两个列表中搜索:一个是类别列表,一个是标签列表。这就是为什么 运行 这个功能需要很长时间的原因。

def get_percentage(l1, l2, sim_score):
    diff = intersection(l1, l2)
    size = len(l1)
    if size != 0:
        perc = (diff/size)
        if perc >= sim_score:
                return True
    else:
        return False

def intersection(lst1, lst2):
    return len(list(set(lst1) & set(lst2)))

def filter_entities(country, city, category, entities, entityId):
    valid_entities = []
    tags = get_tags(entities, entityId)
    for index, i in entities.iterrows():
        if i["country"] == country and i["city"] == city:
            for j in i.categories:
                if j == category:
                    if(get_percentage(i["tags"], tags, 0.80)):
                        valid_entities.append(i.entity_id)

    return valid_entities

你有几个不必要的 for 循环和 if 检查在那里你可以删除,你绝对应该利用 df.loc 从数据框中选择元素(假设 entities 一个 Pandas 数据帧):

def get_percentage(l1, l2, sim_score):
    if len(l1) == 0:
        return False  # shortcut this default case
    else:
        diff = intersection(l1, l2)
        perc = (diff / len(l1))
        return perc >= sim_score  # rather than handling each case separately

def intersection(lst1, lst2):
    return len(set(lst1).intersection(lst2))  # almost twice as fast this way on my machine

def filter_entities(country, city, category, entities, entityId):
    valid_entities = []
    tags = get_tags(entities, entityId)
    # Just grab the desired elements directly, no loops
    entity = entities.loc[(entities.country == county) &
                          (entities.city == city)]
    if category in entity.categories and get_percentage(entity.tags, tags, 0.8):
        valid_entities.append(entity.entity_id)
    return valid_entities

很难肯定地说这会有所帮助,因为我们无法真正 运行 您提供的代码,但这应该可以消除一些低效问题并利用 [=26] 中可用的一些优化=].

根据您的数据结构(即,如果您在上面的 entity 中有多个匹配项),您可能需要对上面的最后三行执行类似的操作:

for ent in entity:
    if category in ent.categories and get_percentage(ent.tags, tags, 0.8):
        valid_entities.append(ent.entity_id)
return valid_entities

第一步是查看 Engineero 的答案,它修复了不必要的 if 和 for 循环。接下来我会建议您是否使用大量输入数据,如果它花费大量时间,应该是这种情况。您可能希望使用 numpy 数组而不是列表来存储数据,因为它更适合大量数据 as seen here. Numpy even beats out Pandas DataFrames as seen here。在某个点之后你应该问问自己效率是否比使用方便更重要Pandas,如果是这样,对于大量数据 Numpy 会更快。