【发布时间】:2018-07-26 14:48:46
【问题描述】:
我想优化这个过滤功能。它在两个列表中搜索:一个是类别,一个是标签。这就是为什么运行此功能需要很长时间的原因。
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
【问题讨论】:
-
对工作代码的审查或改进请求更适合Code Review,而不是 Stack Overflow。有关差异的详细讨论,请参阅A guide to Code Review for Stack Overflow users。
标签: python performance optimization execution-time