如何解决我想优化python中的代码以减少时间
我得到了以下Python代码:
newArr = []
for i in range(0,len(customers)):
sum = 1
for j in range(i+1,len(customers)):
if customers[i] == customers[j] :
sum += 1
sum = (sum/len(customers)) * 100
if sum >= 5:
newArr.append(customers[i])
newArr = set(newArr)
newArr = list(newArr)
newArr.sort()
return newArr
如何优化代码并减少执行时间?
解决方法
据我了解,一旦您发现一种类型的客户包含要添加的客户的5%以上,然后又将其返回(排序)。因此,按类型对客户进行计数并仅返回通过该阈值的客户就足够了-
from collections import Counter
customers_counter = Counter(customers)
threshold = int(5 * len(customers)/100 -1)
output_list = []
customers_counter = {k:v for k,v in customers_counter.items() if v > threshold}
return sorted(customers_counter.keys())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。