我想优化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())

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...