Find 4 values in window size 6 that fit criteria then Add to list until 3 do not fit criteria not include last 3 -> Repeat next window . Python

独自空忆成欢 提交于 2020-01-14 05:55:11

问题


if you have a list of values:

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

and wanted to scan through with a window size of 6 and if 4 out of the 6 were >= 100 then keep scanning until there were 3 in a row that were < 100 and then not include those in the list

so for example with an empty list called results:

results=[]

i would like to append those values that satisfied the criteria into the empty list to get

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

i know i have convert all the strings into integers with int() but that's not the part that i'm having trouble with. i'm having trouble finding the 4 out of the window size 6 that are >= 100, adding that to a list, AND THEN going from where i left off in the window

In the Chou Fasman Algorithm each window size is 6 and if 4 are greater than 100 then all 6 are included and its extended until 4 consecutive values (i'm going to do 3 instead) are less than 100 (those 4 (or 3) are not included) and then the window starts again from that spot making a new list.

so first window would be:

['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')]

I'd really like to use For loops and stay clear of yields and generators if possible but if that's the only way then so be it


回答1:


Would you like to try with this to see if it suits your requirement?

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']]
>>> 


来源:https://stackoverflow.com/questions/8462517/find-4-values-in-window-size-6-that-fit-criteria-then-add-to-list-until-3-do-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!