REALLY need help with iterating a list.

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jun 11 22:43:37 CEST 2007


En Mon, 11 Jun 2007 17:11:23 -0300, Radamand <radamand at gmail.com> escribió:

> On Jun 11, 1:23 pm, Fredrik Lundh <fred... at pythonware.com> wrote:

>> 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




More information about the Python-list mailing list