newb loop problem

D

Dave

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)

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
 
D

Dave

Dave said:
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)

print hitLst
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.

hitNum = 5
which will return:
['a2', 'b2', 'c2', 'd2', 'e2', 'a3']
and append it to the previous one.

['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,

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
 
D

Dave

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


Dave said:
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)
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

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.

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
 
M

marek.rocki

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)

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
 
S

Sion Arrowsmith

Dave said:
hitNum = 0
stopCnt = 6 + hitNum
offSet = 5

for i in range(0,10,1):

The step argument to range defaults to 1: it's tidier to omit it.
Similarly, the start argument defaults to 0, so you can drop that too.

for i in range(10):
for x in range(hitNum,len(inLst), 1):
print hitNum, stopCnt

hitNum and stopCnt are constant in this loop: if you care about
this print statement, move it into the outer loop and stop yourself
drowning in output.
if x == stopCnt: break

If you want to exit the inner loop when x == stopCnt, why not make
that condition part of the loop construct?

for x in range(hitNum, stopCnt):

That said, if you ever see "for i in range(len(lst))" *immediately*
replace it by "for i, x in enumerate(lst)", then go through to body
to see if you really need that i, and if not use "for x in lst",
with slicing if the range is more complex than range(len(lst)). As
you can do here:

for x in inLst[hitNum:stopCnt]:
hitLst.append(x)

And if all you're doing in a for loop is appending to one list from
another, that's just what list.extend does:

hitLst.extend(inLst[hitNum:stopCnt])
hitNum +=offSet
stopCnt+=offSet

Finally, that i in the outer loop isn't being used anywhere. Why
don't you just loop over hitNum? And noticing that stopCnt is
increasing in lock-step with hitNum:

offset = 5

for hitNum in range(0, 10*offset, offset):
hitLst.extend(inLst[hitNum:hitNum+6]
 
D

Dave

Dave said:
hitNum = 0
stopCnt = 6 + hitNum
offSet = 5
for i in range(0,10,1):

The step argument to range defaults to 1: it's tidier to omit it.
Similarly, the start argument defaults to 0, so you can drop that too.

for i in range(10):
for x in range(hitNum,len(inLst), 1):
print hitNum, stopCnt

hitNum and stopCnt are constant in this loop: if you care about
this print statement, move it into the outer loop and stop yourself
drowning in output.
if x == stopCnt: break

If you want to exit the inner loop when x == stopCnt, why not make
that condition part of the loop construct?

for x in range(hitNum, stopCnt):

That said, if you ever see "for i in range(len(lst))" *immediately*
replace it by "for i, x in enumerate(lst)", then go through to body
to see if you really need that i, and if not use "for x in lst",
with slicing if the range is more complex than range(len(lst)). As
you can do here:

for x in inLst[hitNum:stopCnt]:
hitLst.append(x)

And if all you're doing in a for loop is appending to one list from
another, that's just what list.extend does:

hitLst.extend(inLst[hitNum:stopCnt])
hitNum +=offSet
stopCnt+=offSet

Finally, that i in the outer loop isn't being used anywhere. Why
don't you just loop over hitNum? And noticing that stopCnt is
increasing in lock-step with hitNum:

offset = 5

for hitNum in range(0, 10*offset, offset):
hitLst.extend(inLst[hitNum:hitNum+6]

--
\S -- (e-mail address removed) --http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

Excellent, thanks for all your help guys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top