在窗口大小 6 中找到符合条件的 4 个值,然后添加到列表中,直到 3 个不符合标准不包括最后 3 个 -> 重复下一个窗口。蟒



如果您有一个值列表:

values=['130','90','150','123','133','120','160',
        '45','67',
        '55','34','130','120','180','130','10']

,并希望扫描窗口大小为6,如果6个中有4个>= 100,则继续扫描,直到有3个在一行中是<100,然后不包括那些在列表

,例如一个空列表results:

results=[]

我想将满足条件的值附加到空列表中以得到

results=[('130','90','150','123','133','120','160'),
         ('55','34','120','180','130','10')]

我知道我用int()将所有的字符串转换成整数,但这不是我遇到麻烦的部分。我很难找到窗口大小6>= 100的4,将其添加到列表中,然后从我离开的窗口

在Chou Fasman算法中,每个窗口大小为6,如果4大于100,则所有6个都包括在内,并扩展到4个连续值(我要做3代替)小于100(不包括那些4(或3)),然后窗口再次从该位置开始制作新列表。

所以第一个窗口是:

['130','90','150','123','133,'120'] #and more than 4 are greater than
# 100 so that starting point is stored and the next window is checked
['90','150','123','133','120','160'] #again there are 4 greater than 
# 100 so the next window is checked
['150','123','133','120','160','45'] #again
['123','133','120','160','45','67'] #again
['133','120','160','45','67','55'] #stop and assign values '130' to '160'
# into a list and then start the new window from where it left off
Results=[('130','90','150','123','133','120','160')]
['120','160','45','67','55','34'] # skips
['160','45','67','55','34','130'] # skips
['45','67','55','34','130','120'] # skips
['67','55','34','130','120','180'] # skips
['55','34','130','120','180','130'] # new list in Results starts with '55'
['34','130','120','180','130','10'] # sequence ends and this window still 
# fits criteria so include these into the list so the results would now be
Results=[('130','90','150','123','133','120','160'),      
         ('55','34','130','120','180','130','10')]

我真的很想使用For循环并且尽可能避免yield和generator但是如果这是唯一的方法,那就用

您想试试这个,看看它是否符合您的要求?

for i in xrange(0,len(values)):
    results[-1].append(values[i])
    if len(filter(lambda x:int(x)<100,results[-1][-3:])) == 3:
        results.append(results[-1][-2:])
        results[-2]=results[-2][:-3]
        if len(results[-2]) == 0:
            del results[-2]

>>> results
[['130', '90', '150', '123', '133', '120', '160'], ['55', '34', '130', '120', '180', '130', '10']]
>>> 

相关内容

  • 没有找到相关文章

最新更新