REALLY need help with iterating a list.

Fredrik Lundh fredrik at pythonware.com
Mon Jun 11 22:47:17 CEST 2007


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>




More information about the Python-list mailing list