📄 page82.html
字号:
<HTML><HEAD><TITLE>Python Lists and Arrays</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html2150" HREF="page83.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2148" HREF="page81.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2142" HREF="page81.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2152" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION004100000000000000000">Python Lists and Arrays</A></H1><A NAME="secfdsarrays"> </A><P>Probably the most common way to aggregate data in a Python programis to use a Python list.A Python list is an object thatcontains an ordered collection of objects.For example,<PRE>a = [0, 0, 0, 0, 0]</PRE>creates a Python list that comprises five plain integers (all zero)and assigns it to the variable <tt>a</tt>.<P>The elements of a Python list are accessed using integer-valued indices.The first or leftmost element of a Python list always has index zero.Thus, the five elements of list <tt>a</tt> are<tt>a[0]</tt>, <tt>a[1]</tt>, ..., <tt>a[4]</tt>.Python also supports the use of negative indicesto index into a list from the right.The last or rightmost element of a Python list always has index <tt>-1</tt>.Thus, the five elements of list <tt>a</tt> can also be accessed as<tt>a[-5]</tt>, <tt>a[-4]</tt>, ..., <tt>a[-1]</tt>.<P>Python provides a built-in function called <tt>len</tt>that returns the length of any object (that has a length).When applied to a Python list,the <tt>len</tt> function returns the number of elements in that list.Thus, <tt>len(a)</tt> has the value <tt>5</tt>.<P>Python checks at run-timethat the index used to access a list element is valid.Valid indices fall between -<I>n</I> and <I>n</I>-1,where <I>n</I> is the length of the list.If an invalid index expression is used,an <tt>IndexError</tt> exception is raised.<P>It is important to remember that in Python,an assignment statement assigns a name to an object.In particular, the sequence of statements<PRE>a = [0, 0, 0, 0, 0]b = a</PRE>causes the variable <tt>b</tt> to referto the same list object as variable <tt>a</tt>.Furthermore, the sequence of statements<PRE>x = 57a[0] = xa[1] = x</PRE>causes <tt>x</tt>, <tt>a[0]</tt>, and <tt>a[1]</tt>all to refer to the same object.<P>How is a Python list represented in the memory of the computer?The specification of the Python language leaves many of the detailsup to the system implementers[<A HREF="page610.html#vanrossumref">49</A>].However, Figure <A HREF="page82.html#figarray"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates the typical implementation scenario.<P><P><A NAME="2678"> </A><A NAME="figarray"> </A> <IMG WIDTH=575 HEIGHT=262 ALIGN=BOTTOM ALT="figure2562" SRC="img579.gif" ><BR><STRONG>Figure:</STRONG> Memory representation of a Python list.<BR><P><P>A Python list represents a collection of objects.In this case, the objects are all plain integers.The list object actually contains an array of the identities (or addresses)of the objects in the collection.The array elements (the identities)typically occupy consecutive memory locations.That way, given <I>i</I> it is possible to findthe identity of <IMG WIDTH=20 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60393" SRC="img580.gif" > in constant time.<P>On the basis of Figure <A HREF="page82.html#figarray"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,we can now estimate the total storage requiredto represent a Python list.Let <I>S</I>(<I>n</I>) be the total storage (memory) needed torepresent a list of <I>n</I> objects.<I>S</I>(<I>n</I>) is given by<P> <IMG WIDTH=500 HEIGHT=16 ALIGN=BOTTOM ALT="eqnarray2683" SRC="img581.gif" ><P>where the function <IMG WIDTH=69 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60401" SRC="img582.gif" > is the number of bytesused for the memory representationof an instance of an object of type <tt>X</tt>.<P>In the Python virtual machine,object identities (or addresses)are typically represented using fixed-size integers.Hence, <IMG WIDTH=132 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60403" SRC="img583.gif" >.In practice, a Python list may contain additional data items.For example, it is reasonable to expect that there is a datumthat records the position in memory of the first array element.In any event, the overhead associated witha fixed number of additional data items is <I>O</I>(1).Therefore, <I>S</I>(<I>n</I>)=<I>O</I>(<I>n</I>).<P><BR> <HR><UL> <LI> <A NAME="tex2html2153" HREF="page83.html#SECTION004110000000000000000">Extending Python Lists - An Array Class</A><LI> <A NAME="tex2html2154" HREF="page84.html#SECTION004120000000000000000"><tt>__init__</tt> Method</A><LI> <A NAME="tex2html2155" HREF="page85.html#SECTION004130000000000000000"><tt>__copy__</tt> Method</A><LI> <A NAME="tex2html2156" HREF="page86.html#SECTION004140000000000000000"><tt>__getitem__</tt> and <tt>__setitem__</tt> Methods</A><LI> <A NAME="tex2html2157" HREF="page87.html#SECTION004150000000000000000"><tt>Array</tt> Properties</A><LI> <A NAME="tex2html2158" HREF="page88.html#SECTION004160000000000000000">Resizing an Array</A></UL><HR><A NAME="tex2html2150" HREF="page83.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2148" HREF="page81.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2142" HREF="page81.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2152" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -