[英]Quickest way to count number of repeated leading integers in a list
example of list being looked at (list will always be sorted). 查看列表的示例(列表将始终进行排序)。 Size of list is 4^12.
清单的大小为4 ^ 12。 [1,1,1,1, ...... , 1,1,4]
[1,1,1,1,......,1,1,4]
At the moment I have a for loop that iterates through the list with a counter until it finds the first non 1 and then breaks and returns the counter. 目前,我有一个for循环,使用一个计数器循环遍历该列表,直到找到第一个非1值,然后中断并返回该计数器。 This is quicker than using the count function since the list is already sorted.
这比使用计数功能更快,因为列表已经排序。 I am now implementing binary search and I was wondering if anyone can think of a better way to count the number of leading ones in a ordered list.
我现在正在实施二进制搜索,我想知道是否有人可以想到一种更好的方法来计算有序列表中的前导数字。
You can use itertools.groupby
: 您可以使用
itertools.groupby
:
import itertools
s = [1,1,1,1,1,1,4]
runs = [len(list(b)) for a, b in itertools.groupby(s)][0]
Output: 输出:
6
Bisect is built in and seems comparably fast to binary sort; Bisect是内置的,与二进制排序相比似乎更快。
import bisect
import time
import itertools
test = [1] * 100000000
test[len(test) - 1] = 4
start = time.time()
print(bisect.bisect_right(test,1))
print(time.time() - start)
start = time.time()
runs = [len(list(b)) for a, b in itertools.groupby(test)][0]
print(runs)
print(time.time() - start)
start = time.time()
length= len(test)-test[::-1].index(1)
print(length)
print(time.time() - start)
# Bisect
99999999
3.719329833984375e-05
# GroupBy
99999999
1.2406058311462402
# Length
99999999
0.3920767307281494
If you only want to find the count of the first value then you can use this: 如果只想查找第一个值的计数,则可以使用以下方法:
a = [1,1,1,...,1,4]
first_value = a[0]
length= len(a)-a[::-1].index(first_value) #get index starting from the end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.