我有一个包含1或0的列表;没有其他的。我有兴趣找到1,更具体地说,1的开始运行和运行结束的地方(或者在下面的代码中,1的运行的“长度”....它可以是“长度” “该运行或该运行的结束索引位置,因为我可以做数学并计算从开始和结束位置的长度)。 我在哈希中存储1的运行。是否有更快的方式来获得我所拥有的东西而不是我拥有的东西?我还在学习python,我在现实生活中使用的列表要大得多,所以速度很重要。
previous = 0
cnt = 0
startLength = {}
for r in listy:
if previous == 0 and r == 1:
start = cnt
startLength[start] = 1
if previous == 1 and r == 1:
startLength[start] = 1 + cnt - start
previous = r
cnt += 1
for s,l in startLength.iteritems():
print "A run of 1's starts at position %s and lasts %s" % (s,l)
答案 0 :(得分:6)
我可能会将itertools.groupby
用于此
lst = [ 1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0]
from itertools import groupby
from operator import itemgetter
for k,v in groupby(enumerate(lst),key=itemgetter(1)):
if k:
v = list(v)
print v[0][0],v[-1][0]
这将打印1的
组的开始和结束索引答案 1 :(得分:2)
除了@ mgilson的pythonic答案,你也可以尝试这样的事情:
lst = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1]
start, end = False, False
for i, x in enumerate(lst):
if x == 1 and start is False:
start = i
if x == 0 and start is not False and end is False:
end = i-1
if start is not False and end is not False:
print start, end # and len is (end-start+1)
start, end = False, False
if start is not False:
print start, i
输出:
0 4
12 15
22 23
答案 2 :(得分:1)
这是一个稍微有效的解决方案(抱歉它是JavaScript)。关键是只存储一次长度,在你的代码中,每当长度增加一个“startLength [start] = 1 + cnt - start”时你就要进行计算。
使用条件“if previous == 0 and r == 1”而不是“if previous == 1 and r == 1”。我减少了计算量,但我还必须在for循环之后添加一个“if r == 1”来捕捉最后的情况。
var test=[0,1,1,1,0,0,0,1,1,0,0,1,0];
function runs(arr) {
var result = {};
var start = 0;
var previous = 0;
var cnt = 0;
var r = 0;
for(; cnt<arr.length; cnt++) {
var r = arr[cnt];
if(r == 1 && previous == 0)
start = cnt;
if(r == 0 && previous == 1)
result[start] = cnt - start;
previous = r;
}
if(r == 1)
result[start] = cnt - start;
return result;
}
var result = runs(test);
for(var start in result)
console.log("start " + start + " length " + result[start]);
EDIT 2 这是一个python基准测试,显示使用groupby函数(目前这个问题的最佳答案)要慢得多。
from itertools import groupby
from operator import itemgetter
import random
import time
lst = [ 1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0]
def makeList(size):
random.seed()
return [random.randint(0,1) for r in xrange(size)]
def runs1(lst, showOutput):
startLength = {}
for k,v in groupby(enumerate(lst),key=itemgetter(1)):
if k:
v = list(v)
startLength[v[0][0]] = v[-1][0] + 1 - v[0][0]
if showOutput == True:
for s,l in startLength.iteritems():
print s,l
def runs2(lst, showOutput):
previous = 0
cnt = 0
startLength = {}
for r in lst:
if previous == 0 and r == 1:
start = cnt
if previous == 1 and r == 0:
startLength[start] = cnt - start
previous = r
cnt += 1
if r == 1:
startLength[start] = cnt - start
if showOutput == True:
for s,l in startLength.iteritems():
print s,l
testList = makeList(10)
print "slow version"
runs1(testList, True)
print "fast version"
runs2(testList, True)
benchmarkList = makeList(10000)
start = time.time()
runs1(benchmarkList, False)
print "slow ", time.time() - start
start = time.time()
runs2(benchmarkList, False)
print "fast ", time.time() - start
start = time.time()
runs1(benchmarkList, False)
print "slow ", time.time() - start
start = time.time()
runs2(benchmarkList, False)
print "fast ", time.time() - start
start = time.time()
runs1(benchmarkList, False)
print "slow ", time.time() - start
start = time.time()
runs2(benchmarkList, False)
print "fast ", time.time() - start
答案 3 :(得分:-1)
如果将列表转换为字符串,则可以使用正则表达式。
import re
import random
int_list = [random.randint(0,1) for i in xrange(1000000)]
runs = re.findall('1+', ''.join(map(str, int_list) # get a list of one-runs
# Print their lengths.
for run in runs:
print len(run)
# If you really need to know the indexes where the runs are found, instead use
runs = re.finditer('1+', ''.join(map(str, int_list) # get a list of matches
for run in runs:
print 'Start:\t%s' % run.start()
print 'End:\t%s' % run.end()
print 'Length:\t%s' % run.end()-run.start()