【发布时间】:2020-07-21 09:49:27
【问题描述】:
我有一个客户列表,我希望返回一个排序过的客户列表 超过原始列表中的 5% 的时间。以下工作,但我需要优化它。不幸的是,我无法发现如何(大幅)提高时间效率。有什么建议吗?
def mostActive(customers):
unique_customers = set(customers)
count = len(customers)
result = []
for customer in unique_customers:
if customers.count(customer) / count >= .05:
result.append(customer)
return sorted(result)
【问题讨论】:
-
尝试列表理解。它会让你的代码更简单:)
标签: python performance optimization time