Best way to delimit a list?

castironpi at gmail.com castironpi at gmail.com
Tue May 13 09:24:56 EDT 2008


On May 13, 8:17 am, Giuseppe Ottaviano <giu... at gmail.com> wrote:
> On May 13, 2008, at 12:28 PM, dannywebs... at googlemail.com wrote:
>
>
>
>
>
> > Hi - I have a list returned from popen/readlines, and am wondering how
> > to go about iterating over each item which was returned (rather than
> > currently having the whole lot returned).
>
> > so far:
>
> >>>> f=os.open("./get_hostnames").readlines
>
> > returns ['host1 host2 host3 ... hostN\n]'
>
> > i'd like to be in a position to iterate through these, grabbing each
> > host.  I have played with transmuting to a str, and using split, and
> > this works, but I get the subscript brackets from the list output as
> > expected, as the list output is now a string literal, and this is not
> > what I want - and I think it's a bit long-winded to do a search 'n
> > replace on it - hence why I ask in the subject what's the best way.
>
> >>>> f=str(f)
> >>>> f.split()
> > ["['host1","host2", ... ,"hostN\n']"]
>
> If the file is really big, you may want not to construct an actual  
> list with all the words, but instead use an iterator. If you define  
> the function
>
> def ichain(seq):
>         for s in seq:
>                 for x in s: yield x
>
> (which is often useful and I don't think it has been included in  
> itertools) you can iterate lazily on the file:
>
> hosts = ichain(s.split() for s in f)
> for host in hosts:
>         # ...
>
> HTH,
> Giuseppe- Hide quoted text -
>
> - Show quoted text -

I am having trouble following, but I am not an always-rightter.  Was
s.split( ) one of the things you wanted to do to a line, and likely a
really common one?  I'm trying to approach problems impractically.

Now of course, if anything else is going on in the program, you will
need separate threads or separate interpreters/processes.  Does Python
meet sufficiencies on threading?  What file do we have to test it on?



More information about the Python-list mailing list