Python equivalent of Java Vector

hoopy_frood at my-deja.com hoopy_frood at my-deja.com
Tue Oct 3 11:35:49 EDT 2000


Thanks to all for the helpful responses.  I'd buy you all a beer, but
I'm an American, and the beer here isn't much of a reward.

hoopyfrood

In article <uzokmv92j.fsf at ctwd0143.fitlinxx.com>,
  David Bolen <db3l at fitlinxx.com> wrote:
> hoopy_frood at my-deja.com writes:
>
> > "Does Python have something equivalent to Java's Vector?  For
instance,
> > most of the time if you want to use an array you have to declare the
> > size up front.  Java has a Vector class which is an array of objects
> > which grows as you add things to it.
>
> The comment about "most of the time" isn't really true for Python,
> where the default tends to be dynamic more than static structure
> declarations.  Heck, even a class in Python isn't really a fixed
> definition, but is completely malleable at run-time.
>
> > "Just curiousity on my part really, but if you are serious about
using
> > Python productively, you probably need to answer that.  In Java, for
> > instance, if I want an array of String I say "String tmp[]=new
> > String[5]" and I have an array of Strings with 5 slots.  If I didn't
> > know how many Strings I was going to have though, I could say
"Vector
> > tmp=new Vector()" then every time I wanted to add a String, say
"String
> > tmpvalue" I could say "tmp.addElement(tmpvalue)".  Then, I could say
> > "tmp.size()" to see how many elements were in the Vector, and I can
even
> > to a "tmp.elementAt(int x)" to retrieve a value.  So, I think the
> > terminology would be that Java supports dynamic arrays of objects.
I
> > was wondering if Python had the equivalent."
>
> One of the fundamental Python types - a list - works this way.  You
> don't define the overall size of a list, can append to it at will (the
> .append method) in addition to inserting entries at arbitrary
> locations (the .insert method) among other methods, can query the
> length with len(), and a list is a container of a heterogenous set of
> objects, so you can add anything you want to it, including other
> lists.  For retrieval, the objects are just indexed like an array
> ([#]), and you can also extract "slices" of the list (e.g., a range of
> entries) as another list.  It's one of a general class of types called
> sequences.
>
> If you need keyed access to a heterogenous set of objects, you would
> use a dictionary type (a class of types called mappings), which would
> let you insert and retrieve the information based on a key, which
> itself can be a fairly broad range of object types.
>
> You might point the person who originally sourced the query to take a
> quick glance at the first few sections of chapter 2 of the library
> reference (Built-in Types, Exceptions and Functions) - it's short, and
> might pique their interest :-)
>
> --
> -- David
> --
>
/-----------------------------------------------------------------------
\
>  \               David Bolen            \   E-mail: db3l at fitlinxx.com
 /
>   |             FitLinxx, Inc.            \  Phone: (203) 708-5192
|
>  /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150
 \
>
\-----------------------------------------------------------------------
/
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list