473,386 Members | 1,785 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,386 software developers and data experts.

REALLY need help with iterating a list.

This has been driving me buggy for 2 days, i need to be able to
iterate a list of items until none are left, without regard to which
items are removed. I'll put the relevant portions of code below,
please forgive my attrocious naming conventions.
Basically i'm trying to spin up some subprocesses that will ping a
group of servers and then wait until all of them have completed (good
or bad), store the ping result and the return code, and move on.
The problem comes in the second block, when i try to iterate the
list of servers and remove the ones that are finished, for some reason
Python appears to re-index the list when I remove an item and the next
step through the loop it cant find the item its expecting because the
indexes have changed.
Any assistance would be appreciated...

================================================== ===========================
for server in serverlist:
ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
shell=True, stdout=subprocess.PIPE)

while len(serverlist) 0:
for server in serverlist:
if ping[server].returncode==None:
ping[server].poll()
else:
pingresult[server] = ping[server].stdout.read()
pingreturncode[server] = ping[server].returncode
serverlist.remove(server)

Jun 11 '07 #1
7 1242
alg
Reverse iteration should do the trick, if I understand your problem:

for server in reversed(serverlist):
...
else:
serverlist.remove(server)

On Jun 11, 11:30 am, Radamand <radam...@gmail.comwrote:
This has been driving me buggy for 2 days, i need to be able to
iterate a list of items until none are left, without regard to which
items are removed. I'll put the relevant portions of code below,
please forgive my attrocious naming conventions.
Basically i'm trying to spin up some subprocesses that will ping a
group of servers and then wait until all of them have completed (good
or bad), store the ping result and the return code, and move on.
The problem comes in the second block, when i try to iterate the
list of servers and remove the ones that are finished, for some reason
Python appears to re-index the list when I remove an item and the next
step through the loop it cant find the item its expecting because the
indexes have changed.
Any assistance would be appreciated...

================================================== ===========================
for server in serverlist:
ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
shell=True, stdout=subprocess.PIPE)

while len(serverlist) 0:
for server in serverlist:
if ping[server].returncode==None:
ping[server].poll()
else:
pingresult[server] = ping[server].stdout.read()
pingreturncode[server] = ping[server].returncode
serverlist.remove(server)

Jun 11 '07 #2
On Jun 11, 11:30 am, Radamand <radam...@gmail.comwrote:
This has been driving me buggy for 2 days, i need to be able to
iterate a list of items until none are left, without regard to which
items are removed. I'll put the relevant portions of code below,
please forgive my attrocious naming conventions.
Basically i'm trying to spin up some subprocesses that will ping a
group of servers and then wait until all of them have completed (good
or bad), store the ping result and the return code, and move on.
The problem comes in the second block, when i try to iterate the
list of servers and remove the ones that are finished, for some reason
Python appears to re-index the list when I remove an item and the next
step through the loop it cant find the item its expecting because the
indexes have changed.
Any assistance would be appreciated...

================================================== ===========================
for server in serverlist:
ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
shell=True, stdout=subprocess.PIPE)

while len(serverlist) 0:
for server in serverlist:
if ping[server].returncode==None:
ping[server].poll()
else:
pingresult[server] = ping[server].stdout.read()
pingreturncode[server] = ping[server].returncode
serverlist.remove(server)
How about something like this?

while serverlist:
server = serverlist.pop(0)
pinger = ping[server]
if pinger.returncode==None:
pinger.poll()
serverlist.append(server)
else:
pingresult[server] = pinger.stdout.read()
pingreturncode[server] = pinger.returncode

Basic idea: as long as there are servers in the list, pop the first
one out of the list, see if it's done, and if it isn't, put it back on
the end of the list.

Jun 11 '07 #3
infidel wrote:
How about something like this?

while serverlist:
server = serverlist.pop(0)
pinger = ping[server]
if pinger.returncode==None:
pinger.poll()
serverlist.append(server)
else:
pingresult[server] = pinger.stdout.read()
pingreturncode[server] = pinger.returncode

Basic idea: as long as there are servers in the list, pop the first
one out of the list, see if it's done, and if it isn't, put it back on
the end of the list.
here's a simple variation of that, which is a bit more efficient, and
perhaps also a bit easier to use in the general case:

while serverlist:
still_active = []
for server in serverlist:
pinger = ping[server]
if pinger.returncode is None:
pinger.poll()
still_active.append(server)
else:
pingresult[server] = pinger.stdout.read()
pingreturncode[server] = pinger.returncode
serverlist = still_active

</F>

Jun 11 '07 #4
On Jun 11, 1:23 pm, Fredrik Lundh <fred...@pythonware.comwrote:
infidel wrote:
How about something like this?
while serverlist:
server = serverlist.pop(0)
pinger = ping[server]
if pinger.returncode==None:
pinger.poll()
serverlist.append(server)
else:
pingresult[server] = pinger.stdout.read()
pingreturncode[server] = pinger.returncode
Basic idea: as long as there are servers in the list, pop the first
one out of the list, see if it's done, and if it isn't, put it back on
the end of the list.

here's a simple variation of that, which is a bit more efficient, and
perhaps also a bit easier to use in the general case:

while serverlist:
still_active = []
for server in serverlist:
pinger = ping[server]
if pinger.returncode is None:
pinger.poll()
still_active.append(server)
else:
pingresult[server] = pinger.stdout.read()
pingreturncode[server] = pinger.returncode
serverlist = still_active

</F>
Thats an interesting approach but, if the returncode for a given
server is None say, 20 times in a row you will have append'ed that
server to the list 20 times, i suppose you could check the list to see
if its already there but thats a bit kludgey...

also, the line "pinger = ping[server]" would have to be extracted from
this loop otherwise your going to ping the same server repeatedly
until it answers...

Jun 11 '07 #5
On Jun 11, 12:59 pm, infidel <saint.infi...@gmail.comwrote:
On Jun 11, 11:30 am, Radamand <radam...@gmail.comwrote:
This has been driving me buggy for 2 days, i need to be able to
iterate a list of items until none are left, without regard to which
items are removed. I'll put the relevant portions of code below,
please forgive my attrocious naming conventions.
Basically i'm trying to spin up some subprocesses that will ping a
group of servers and then wait until all of them have completed (good
or bad), store the ping result and the return code, and move on.
The problem comes in the second block, when i try to iterate the
list of servers and remove the ones that are finished, for some reason
Python appears to re-index the list when I remove an item and the next
step through the loop it cant find the item its expecting because the
indexes have changed.
Any assistance would be appreciated...
================================================== ===========================
for server in serverlist:
ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
shell=True, stdout=subprocess.PIPE)
while len(serverlist) 0:
for server in serverlist:
if ping[server].returncode==None:
ping[server].poll()
else:
pingresult[server] = ping[server].stdout.read()
pingreturncode[server] = ping[server].returncode
serverlist.remove(server)

How about something like this?

while serverlist:
server = serverlist.pop(0)
pinger = ping[server]
if pinger.returncode==None:
pinger.poll()
serverlist.append(server)
else:
pingresult[server] = pinger.stdout.read()
pingreturncode[server] = pinger.returncode

Basic idea: as long as there are servers in the list, pop the first
one out of the list, see if it's done, and if it isn't, put it back on
the end of the list.
I like this idea, ill try it out asap.

ok, tried it, works perfectly!! It never occurred to me to use pop to
pull one off and put it back on if it wasnt done, very nice! Thank
You!!

Jun 11 '07 #6
En Mon, 11 Jun 2007 17:11:23 -0300, Radamand <ra******@gmail.comescribió:
On Jun 11, 1:23 pm, Fredrik Lundh <fred...@pythonware.comwrote:
>here's a simple variation of that, which is a bit more efficient, and
perhaps also a bit easier to use in the general case:

while serverlist:
still_active = []
for server in serverlist:
pinger = ping[server]
if pinger.returncode is None:
pinger.poll()
still_active.append(server)
else:
pingresult[server] = pinger.stdout.read()
pingreturncode[server] = pinger.returncode
serverlist = still_active

</F>

Thats an interesting approach but, if the returncode for a given
server is None say, 20 times in a row you will have append'ed that
server to the list 20 times, i suppose you could check the list to see
if its already there but thats a bit kludgey...
Read the code again and notice that there are TWO lists involved. There is
at most one append per server - unless there are duplicates in the
original list, the new list won't have any.
also, the line "pinger = ping[server]" would have to be extracted from
this loop otherwise your going to ping the same server repeatedly
until it answers...
Uh...?

--
Gabriel Genellina

Jun 11 '07 #7
Radamand wrote:
>while serverlist:
still_active = []
for server in serverlist:
pinger = ping[server]
if pinger.returncode is None:
pinger.poll()
still_active.append(server)
else:
pingresult[server] = pinger.stdout.read()
pingreturncode[server] = pinger.returncode
serverlist = still_active

</F>

Thats an interesting approach but, if the returncode for a given
server is None say, 20 times in a row you will have append'ed that
server to the list 20 times

also, the line "pinger = ping[server]" would have to be extracted from
this loop otherwise your going to ping the same server repeatedly
until it answers...
did you miss that there are two loops here? the inner loop loops over
the available servers, and add servers that needs to be checked again to
a *new* list. that list is then assigned to the original serverlist
variable.

</F>

Jun 11 '07 #8

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

Similar topics

0
by: aredo3604gif | last post by:
I have coded a serie of singly linked lists in ANSI C which I have to use. The lists are then stored in a serie of buckets with chained hash table technique. In the various lists there are nodes...
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
5
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
20
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
9
by: Andreas Schmitt | last post by:
I am somewhat new to C# and I ran into a problem in a small program I am writing for teaching myself. I am handling a list ob objects and I want to delete some of them inside a loop like in: ...
1
by: Ken Pu | last post by:
Hi all, I observed an interesting yet unpleasant variable scope behaviour with list comprehension in the following code: print print x It outputs:
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
10
by: kj | last post by:
Hi. I'd like to port a Perl function that does something I don't know how to do in Python. (In fact, it may even be something that is distinctly un-Pythonic!) The original Perl function takes...
2
by: Terry Reedy | last post by:
SUBHABRATA, I recommend you study this excellent response carefully. castironpi wrote: It starts with a concrete test case -- an 'executable problem statement'. To me, this is cleared and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.