识别列表中良好数据的开始 [英] Identifying the start of good data in a list
问题描述
我有一个以零开头的列表,有零星数据,然后有好b / b
好数据。我将数据转好的点定义为
第一个索引,其中非零条目后跟至少4个
个连续的非零数据项(即一周的非零值
数据)。例如,如果我的列表是[0,0,1,0,1,2,3,4,5,6,7,8,
9],我会定义数据变好4(1
后跟2,3,4,5)。
我有一个简单的算法来识别这个变化点,但是它看起来很好
raw:有更干净,更优雅的方式吗?
flag = True
i = -1
j = 0
而flag和i< len(retHist)-1:
i + = 1
如果retHist [i] == 0:
j = 0
否则:
j + = 1
如果j == 5:
flag = False
del retHist [:i-4]
预先感谢您的帮助
Thomas Philips
我找到的第一个解决方案,在几个doctests旁边没有经过多少测试:
来自itertools import islice
def start_good1(alist,good_ones = 4):
"""
对Python来说可能更有效
>> start_good = start_good1
start_good([0,0,1,0,1,2] ,3,4,5,6,7,8,9])
4
>> start_good([])
-1
>> start_good([0,0])
-1
>> start_good([0,0,0])
-1
>> start_good( [0,0,0,0,1])$ b $ b
-1
>> start_good([0,0,1,0,1,2,3])
-1
>> start_good([0,0,1,0,1,2,3,4])
4
>> start_good([0,0,1,0,1,2, 3,4,5)
4
>> start_good([1,2,3,4])
0
>> start_good([1,2,3] )
-1
>> start_good([0,0,1,0,1,2,0,4])
-1
"""
for x in xrange(len(alist) - good_ones + 1):
if all(islice(alist,i,i + good_ones)):
返回i
返回-1
def start_good2(alist,good_ones = 4):
"""
对Psyco来说可能效率更高
>> start_good = start_good2
start_good([0,0,1,0,1,2,3,4,5,6,7,8,9])
4
>> start_good([ ])
-1
>> start_good([0,0])
-1
>> start_good([0,0,0])
-1
>> start_good([0,0,0, 0,1])$ b $ b
-1
>> start_good([0,0,1,0,1,2,3])
-1
>> start_good([ 0,0,1,0,1,2,3,4])
4
>> start_good([0,0,1,0,1,2,3,4,5])
4
>> start_good([ 1,2,3,4])
0
>> start_good([1,2,3])
-1
>> start_good([0,0,1,1 ,0,1,2,0,4])
-1
""
n_good = 0
for i,el in enumerate(alist):
if alist [i]:
如果n_good == good_ones:
返回i - good_ones
否则:
n_good + = 1
:否则:
n_good = 0
如果n_good == good_ones:
返回len(alist) - good_ones
:否则:
返回-1
如果__name__ ==" __ main __":
import doctest
doctest .testmod()
打印Doctests done\\\
再见,
bearophile
对不起,在Psyco版本中替换这一行:
for i,el in enumerate(alist):
With :
$ x $ b for x in xrange(len(alist)):
因为Psyco没有很好地消化枚举。
再见,
bearophile
tk * ***@hotmail.com 写道:
我有一个以零开头的列表,有零星的数据,然后有
好的数据。我将数据转好的点定义为
第一个索引,其中非零条目后跟至少4个
个连续的非零数据项(即一周的非零值
数据)。例如,如果我的列表是[0,0,1,0,1,2,3,4,5,6,7,8,
9],我会定义数据变好4(1
后跟2,3,4,5)。
我有一个简单的算法来识别这个变化点,但是它看起来很好
raw:有更干净,更优雅的方式吗?
flag = True
i = -1
j = 0
而flag和i< len(retHist)-1:
i + = 1
如果retHist [i] == 0:
j = 0
否则:
j + = 1
如果j == 5:
flag = False
del retHist [:i-4]
预先感谢您的帮助
Thomas Philips
-
http://mail.python.org / mailman / listinfo / python-list
也许会这样做?
reHist = [0, 0,1,0,1,2,3,4,5,6,7,8,9]
count = 0
for i,d in enumerate( reHist):
如果d == 0:
count = 0
else:
count + = 1
如果算数== 5:
休息
否则:
提高异常(未找到数据) )
reHist = reHist [i-4:]
print reHist
-Matt
I have a list that starts with zeros, has sporadic data, and then has
good data. I define the point at which the data turns good to be the
first index with a non-zero entry that is followed by at least 4
consecutive non-zero data items (i.e. a week''s worth of non-zero
data). For example, if my list is [0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
9], I would define the point at which data turns good to be 4 (1
followed by 2, 3, 4, 5).
I have a simple algorithm to identify this changepoint, but it looks
crude: is there a cleaner, more elegant way to do this?
flag = True
i=-1
j=0
while flag and i < len(retHist)-1:
i += 1
if retHist[i] == 0:
j = 0
else:
j += 1
if j == 5:
flag = False
del retHist[:i-4]
Thanks in advance for your help
Thomas Philips解决方案First solutions I have found, not much tested beside the few doctests:
from itertools import islice
def start_good1(alist, good_ones=4):
"""
Maybe more efficient for Python
>>start_good = start_good1
start_good([0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
4
>>start_good([])
-1
>>start_good([0, 0])
-1
>>start_good([0, 0, 0])
-1
>>start_good([0, 0, 0, 0, 1])
-1
>>start_good([0, 0, 1, 0, 1, 2, 3])
-1
>>start_good([0, 0, 1, 0, 1, 2, 3, 4])
4
>>start_good([0, 0, 1, 0, 1, 2, 3, 4, 5])
4
>>start_good([1, 2, 3, 4])
0
>>start_good([1, 2, 3])
-1
>>start_good([0, 0, 1, 0, 1, 2, 0, 4])
-1
"""
for i in xrange(len(alist) - good_ones + 1):
if all(islice(alist, i, i+good_ones)):
return i
return -1
def start_good2(alist, good_ones=4):
"""
Maybe more efficient for Psyco
>>start_good = start_good2
start_good([0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
4
>>start_good([])
-1
>>start_good([0, 0])
-1
>>start_good([0, 0, 0])
-1
>>start_good([0, 0, 0, 0, 1])
-1
>>start_good([0, 0, 1, 0, 1, 2, 3])
-1
>>start_good([0, 0, 1, 0, 1, 2, 3, 4])
4
>>start_good([0, 0, 1, 0, 1, 2, 3, 4, 5])
4
>>start_good([1, 2, 3, 4])
0
>>start_good([1, 2, 3])
-1
>>start_good([0, 0, 1, 0, 1, 2, 0, 4])
-1
"""
n_good = 0
for i, el in enumerate(alist):
if alist[i]:
if n_good == good_ones:
return i - good_ones
else:
n_good += 1
else:
n_good = 0
if n_good == good_ones:
return len(alist) - good_ones
else:
return -1
if __name__ == "__main__":
import doctest
doctest.testmod()
print "Doctests done\n"
Bye,
bearophile
Sorry, in the Psyco version replace this line:
for i, el in enumerate(alist):
With:
for i in xrange(len(alist)):
because Psyco doesn''t digest enumerate well.
Bye,
bearophile
tk****@hotmail.com wrote:I have a list that starts with zeros, has sporadic data, and then has
good data. I define the point at which the data turns good to be the
first index with a non-zero entry that is followed by at least 4
consecutive non-zero data items (i.e. a week''s worth of non-zero
data). For example, if my list is [0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
9], I would define the point at which data turns good to be 4 (1
followed by 2, 3, 4, 5).
I have a simple algorithm to identify this changepoint, but it looks
crude: is there a cleaner, more elegant way to do this?
flag = True
i=-1
j=0
while flag and i < len(retHist)-1:
i += 1
if retHist[i] == 0:
j = 0
else:
j += 1
if j == 5:
flag = False
del retHist[:i-4]
Thanks in advance for your help
Thomas Philips
--
http://mail.python.org/mailman/listinfo/python-list
Maybe this will do?
reHist = [0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
count = 0
for i, d in enumerate(reHist):
if d == 0:
count = 0
else:
count += 1
if count == 5:
break
else:
raise Exception("No data found")
reHist = reHist[i-4:]
print reHist
-Matt
这篇关于识别列表中良好数据的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!