473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

newb loop problem

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
Aug 13 '08 #1
7 1060
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
Aug 13 '08 #2
On Aug 13, 12:35 am, Larry Bates <larry.ba...@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


Aug 13 '08 #3
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...@gmail.comwrote:
On Aug 13, 12:35 am, Larry Bates <larry.ba...@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
Aug 13 '08 #4
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
Aug 13 '08 #5
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...@gmail.comwrote:
>On Aug 13, 12:35 am, Larry Bates <larry.ba...@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
Aug 13 '08 #6
Dave <Nu****@gmail.comwrote:
>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 -- si***@chiark.greenend.org.uk -- 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
Aug 13 '08 #7
On Aug 13, 8:46 am, Sion Arrowsmith <si...@chiark.greenend.org.uk>
wrote:
Dave <Nun...@gmail.comwrote:
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 -- si...@chiark.greenend.org.uk --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.
Aug 15 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nightsaber | last post by:
<script language="JavaScript"> <!-- hide me var the_number = prompt("how many words (3-5 is good)?", "4"); var the_string = ""; var a_word; for (loop = 0; loop < the_number; loop++) {...
3
by: claudel | last post by:
Hi I have a newb PHP/Javascript question regarding checkbox processing I'm not sure which area it falls into so I crossposted to comp.lang.php and comp.lang.javascript. I'm trying to...
4
by: javascript_noob12 | last post by:
Hi. I am trying to create a CGI script that will use Perl to read a text file to get a list of values, then turn that list into a menu on a website. The menu will be a form with a JavaScript...
20
by: Chad Everett | last post by:
Hi all, I am new to the group. Trying to learn Python programming on my own. I am working through Michael Dawson's Book Python Programming for the absolute beginner. I am tring to write a...
10
by: ASiF | last post by:
Hi i am completely novice at programming, would appreciate some help. The program needs to be able to take a set of up to a 100 numbers (between 1 to 9) "inputted" at the prompt and then assign...
2
by: Jo Segers | last post by:
Hi, I am learning C# and I get a compilerer error below, but I do not understand why. Can somebody explain this? Thanx in advance. jo.
3
by: you | last post by:
Ok, I hope that I can 'splain this right. I am very new so bear with me. I have a treeview with its main node being a user selected folder. In this treeview you can only see subfolders and two...
4
by: ProvoWallis | last post by:
I'm totally stumped by this problem so I'm hoping someone can give me a little advice or point me in the right direction. I have a file that looks like this: <SC>APPEAL<XC>40-24; 40-46; 42-46;...
14
by: manstey | last post by:
Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.