newb loop problem

marek.rocki at wp.pl marek.rocki at wp.pl
Wed Aug 13 07:54:15 EDT 2008


Dave napisał(a):
> 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

It seems you're going through a lot of trouble just to construct the
list by hand. Why don't you try slicing?

> 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"
> in_lst = d.split()
> hit_lst = in_lst[0:6]
> hit_lst_2 = in_lst[5:11]

Regards,
Marek



More information about the Python-list mailing list