accessing front of a list 
Author Message
 accessing front of a list

This is probably a fairly newbie question - and also because I am
coming from a Perl background. I was wondering if there is a python
equivilant to Perls shift operator?

There seem to be many ways of getting access to the end of an array -
pop() append() etc but I couldn't find anyting to shift off the front like
when
reading in argv.

This may be becausde I was only learning from the online tutorial/docs which
I have't finished going through. Are there any books worth buying -
particularly
O'Reilley ones? I have seen Learning Python but don't know if this will
cover much more
(although the Perl Llama book isalways useful for Perl), the Programming
Python
and Python Cookbook and the Standalrd Library book. It would be nice if
there was a
"in a Nutshell" one.

thanks

Simon
--
http://www.*-*-*.com/



Sun, 15 May 2005 08:50:35 GMT  
 accessing front of a list

Quote:

> This is probably a fairly newbie question - and also because I am
> coming from a Perl background. I was wondering if there is a Python
> equivilant to Perls shift operator?

> There seem to be many ways of getting access to the end of an array -
> pop() append() etc but I couldn't find anyting to shift off the front like
> when
> reading in argv.

You probably want pop(0):

 >>> a = range(5)
 >>> x = a.pop(0)
 >>> x
0
 >>> a
[1, 2, 3, 4]

...but destructive processing of lists this way is relatively rare, IMHO.

Quote:
> This may be becausde I was only learning from the online tutorial/docs which
> I have't finished going through. Are there any books worth buying -
> particularly
> O'Reilley ones? I have seen Learning Python but don't know if this will
> cover much more
> (although the Perl Llama book isalways useful for Perl), the Programming
> Python
> and Python Cookbook and the Standalrd Library book. It would be nice if
> there was a
> "in a Nutshell" one.

People are working on it...

--
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
Kaa:: http://www.awaretek.com/nowak/kaa.html



Sun, 15 May 2005 09:10:33 GMT  
 accessing front of a list

Quote:

> This is probably a fairly newbie question - and also because I am
> coming from a Perl background. I was wondering if there is a Python
> equivilant to Perls shift operator?

> There seem to be many ways of getting access to the end of an array -
> pop() append() etc but I couldn't find anyting to shift off the front like
> when
> reading in argv.

You can pop() from either end of a list. pop(0) gives you the head, pop()
or pop(-1) give you the end.

But when accessing a list from front to rear, I find my self using
constructs like 'for x in l:...' or the functional programming tools
map(), filter(), reduce(), etc.

--
Doug Fort, Programmer
http://www.dougfort.net



Sun, 15 May 2005 09:15:26 GMT  
 accessing front of a list

Quote:

> You probably want pop(0):

>  >>> a = range(5)
>  >>> x = a.pop(0)
>  >>> x
> 0
>  >>> a
> [1, 2, 3, 4]

> ...but destructive processing of lists this way is relatively rare,
> IMHO.

It's also worth noting that it's an O(n) operation, since the list
elements have to be shuffled down when inserting or deleting one.  If
you're iterating over the whole sequence, that makes it O(n^2).

If that's the case, you're probably better off reversing the list (an
O(n) operation) and then popping them off the end (with L.pop()), which
is an O(1) operation.  That gives you two separate O(n) operations,
instead of an O(n) operation within an O(n) operation (which makes it
O(n^2)).

--

 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Everyone wants to look good at his own funeral.
\__/ Louis Wu
    PyUID / http://www.alcyone.com/pyos/uid/
 A module for generating "unique" IDs in Python.



Sun, 15 May 2005 09:24:14 GMT  
 accessing front of a list
On Wed, 27 Nov 2002 00:50:35 -0000, "Simon Bunker"
(edit)

Quote:
>Are there any books worth buying -
>particularly
>O'Reilley ones? I have seen Learning Python but don't know if this will
>cover much more
>(although the Perl Llama book isalways useful for Perl), the Programming
>Python
>and Python Cookbook and the Standalrd Library book. It would be nice if
>there was a
>"in a Nutshell" one.

Python is a "smaller" language.  You can use the O'Reilly Pocket
Reference in much the same way you used the Perl Nutshell book, in my
experience.

There was a big argument in the newsgroup earlier about whether it
makes sense to call a language "small" or "smaller".  It "feels"
smaller to me.  Of course, your perception is the only one that
counts, but try the Pocket Reference first, and see if that is the
only hardcopy reference you need.

Manuel



Sun, 15 May 2005 09:36:22 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Access commands list and vars list from C API

2. how to bind data to front panel object from ms-access

3. Access Sub.vi's from a front menu button system at any time

4. accessing queue-front (scheme48/scsh)

5. Showing Picture in front of List control

6. adding letters to list front

7. Adding item in front of a list

8. Adding item in front of a list

9. front pannel inside another front pannel

10. Can I use ActiveX to embed a front-panel inside another front-panel

11. list files not accessed

12. Mailing list access to comp.lang.clarion?

 

 
Powered by phpBB® Forum Software