我正在尝试从嵌套列表中以以下形式获取5个最频繁的列表:
[['16.37.123.153','119.222.456.130','38673','161','17','62','4646']
['16.37.456.153','119.222.123.112','56388','161','17','62','4646']..]
目前我正在使用:
for x in xrange(5):
counter = 0
set = L[0]
for i in L:
amount_times = L.count(i)
if amount_times > counter:
counter = amount_times
set = i
while L.count(set) > 0:
L.remove(set)
countermfi.append(counter)
mostfrequent.append(set)
print x
问题是,对于大型列表,运行时确实不好。有人知道我如何用其他解决方案更快地解决该问题吗?这是O(n ^ 2),我需要类似O(n)的东西。
问候:)