Re: Best way to delimit a list?

2008-05-13 Thread dannywebster
On May 13, 11:28 am, [EMAIL PROTECTED] 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']"]
>
> Any help is highly appreciated
>
> ta
>
> dan.

I did indeed mean "os.popen", no "os.open"

--
http://mail.python.org/mailman/listinfo/python-list


Best way to delimit a list?

2008-05-13 Thread dannywebster
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']"]


Any help is highly appreciated

ta

dan.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to delimit a list?

2008-05-13 Thread dannywebster
On May 13, 11:51 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
>
> You meant readlines(), I presume. A file acts as its own iterator:
>
> f=os.open("./get_hostnames")
> try:
>for line in f:
>  # do something with line
> finally:
>f.close()
>
> --
> Gabriel Genellina

Hi - thank you for your reply.

I meant:

f=os.popen("./get_hostnames").readlines()

So f is a list, rather than a file object, of which os.open would have
returned (my initial typo redirected the missive of this post, sorry!)

cheers


--
http://mail.python.org/mailman/listinfo/python-list


Grabbing previous iteration in a dict

2008-05-09 Thread dannywebster
Hello all,

I have a dictionary of which i'm itervalues'ing through, and i'll be
performing some logic on a particular iteration when a condition is
met with trusty .startswith('foo').  I need to grab the previous
iteration if this condition is met.  I can do something with an extra
var to hold every iteration while iterating, but this is hacky and not
elegant.

Is there a mechanism whereby I can just index the dict value
subscripts like in an array? I could just rewind by 1 if so.

Cheerz

dan.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Grabbing previous iteration in a dict

2008-05-09 Thread dannywebster
On May 9, 10:48 am, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > I have a dictionary of which i'm itervalues'ing through, and i'll be
> > performing some logic on a particular iteration when a condition is
> > met with trusty .startswith('foo').  I need to grab the previous
> > iteration if this condition is met.  I can do something with an extra
> > var to hold every iteration while iterating, but this is hacky and not
> > elegant.
>
> You cannot rely on the elements of a dictionary being in any
> particular order (dicts are internally hash tables), so the above
> is almost certainly ont what you want.


Hi - thanks for your reply.  How about if I made the dict into a list
(of
which I have done).  How would I then reference the previous item?
Can they
be indexed?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Grabbing previous iteration in a dict

2008-05-09 Thread dannywebster
On May 9, 4:09 pm, Paul Hankin <[EMAIL PROTECTED]> wrote:
> On May 9, 10:10 am, [EMAIL PROTECTED] wrote:
>
> > Hello all,
>
> > I have a dictionary of which i'm itervalues'ing through, and i'll be
> > performing some logic on a particular iteration when a condition is
> > met with trusty .startswith('foo').  I need to grab the previous
> > iteration if this condition is met.  I can do something with an extra
> > var to hold every iteration while iterating, but this is hacky and not
> > elegant.
>
> Often when you're iterating, 'hacky' code can be lifted out into a
> separate generator, keeping the details of the hacks nicely away from
> your code. That's true here...
>
> def iterprevious(seq):
> """Generate pairs of (previous element, current element)
> from seq."""
> last = None
> for x in iter(seq):
> yield last, x
> last = x
>
> Then, when you want use it...
>
> for previous, (key, value) in iterprevious(d.iteritems()):
> ... In the loop, previous will either be None if we're on the
> first element, otherwise (previous_key, previous_value).
>
> --
> Paul Hankin


Hi all - some good stuff here.  Thanks for the info, I have a few
ideas
to play with.

thanks again

dan.

--
http://mail.python.org/mailman/listinfo/python-list