newb loop problem

Larry Bates larry.bates at websafe.com`
Wed Aug 13 08:36:49 EDT 2008


Dave wrote:
> arrrggg, now I feel really dumb..
> 
> hitNum = 0
> stopCnt = 6 + hitNum
> offSet = 5
> 
> for i in range(0,10,1):
> 
> 	for x in range(hitNum,len(inLst), 1):
> 		print hitNum, stopCnt
> 		if x == stopCnt: break
> 		hitLst.append(inLst[x])
> 	hitNum +=offSet
> 	stopCnt+=offSet
> print hitLst
> 
> 
> Beers, Dave
> 
> 
> On Aug 13, 12:58 am, Dave <Nun... at gmail.com> wrote:
>> On Aug 13, 12:35 am, Larry Bates <larry.ba... at websafe.com`> wrote:
>>
>>
>>
>>> Dave wrote:
>>>> Hey there, having a bit of problem iterating through lists before i go
>>>> on any further, here is
>>>> a snip of the script.
>>>> --
>>>> d = "a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5
>>>> c5 d5 e5"
>>>> inLst = d.split()
>>>> hitLst = []
>>>> hitNum = 0
>>>> stopCnt = 6 + hitNum
>>>> for i in range(hitNum,len(inLst), 1):
>>>>    if i == stopCnt: break
>>>>    hitLst.append(inLst[i])
>>>> print hitLst
>>>> --
>>>> $ python helper.py
>>>> ['a1', 'b1', 'c1', 'd1', 'e1', 'a2']
>>>> This works fine for my purposes, what I need is an outer loop that
>>>> goes through the original list again and appends my next request.
>>>> ie.
>>>> hitNum = 5
>>>> which will return:
>>>> ['a2', 'b2', 'c2', 'd2', 'e2', 'a3']
>>>> and append it to the previous one.
>>>> ie:
>>>> ['a1', 'b1', 'c1', 'd1', 'e1', 'a2']
>>>> ['a2', 'b2', 'c2', 'd2', 'e2', 'a3']
>>>> not really sure how to do this right now though, been trying several
>>>> methods with no good results.
>>>> btw, just creating lagged values (sort of shift registers) on the
>>>> incoming signal
>>>> Many thanks,
>>>> Dave
>>> Dave,
>>> You are going to need to supply us with more info to help.
>>> 1) Are you always going to want to get 6 elements from the list
>>> 2) Are you always going to want to step by 5 elements as your offset each time
>>> through the outer loop?
>>> 3) Is your requirement just to split inLst into equal length lists and append
>>> them together into a list?
>>> Depending on your answers this might be quite easy.
>>> -Larry
>> Hi Larry, well to answer your questions.
>>
>> 1) Are you always going to want to get 6 elements from the list
>>
>> yes for this example, but this will change depending on the length of
>> the sig.
>> hitNum will be changing depending on what element I need to lag ie:
>>
>> if hitNum = 1
>>
>> ['b1', 'c1', 'd1', 'e1', 'a2', 'b2']
>>
>> so I will be lagging the b elements.
>>
>> 2) Are you always going to want to step by 5 elements as your offset
>> each time
>> through the outer loop?
>>
>> I think I just answered this
>>
>> 3) Is your requirement just to split inLst into equal length lists and
>> append
>> them together into a list?
>>
>> yes
>>
>> Thanks for all your help,
>>
>> Dave
> 

This can also be written as:

d = "a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 " \
     "a4 b4 c4 d4 e4 a5 b5 c5 d5 e5"
inLst = d.split()
sliceLen = 6
step = 5
offset = 1
hitLst = list()
for n in xrange(len(inLst)/step):
     start = offset + n * step
     hitLst.append(inLst[offset:offset + sliceLen])

print hitLst


-Larry



More information about the Python-list mailing list