page93.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 49 行

HTML
49
字号
<HTML><HEAD><TITLE>MultiDimensionalArray class    __getitem__ and __setitem__ Methods</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="tex2html2282" HREF="page94.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2280" HREF="page89.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2274" HREF="page92.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2284" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION004240000000000000000"><tt>MultiDimensionalArray</tt> class    <tt>__getitem__</tt> and <tt>__setitem__</tt> Methods</A></H2><P>The elements of a multi-dimensional array are accessedusing the <tt>__getitem__</tt> and <tt>__setitem__</tt> methodsof the <tt>MultiDimensionalArray</tt> class.For example,you can access the  <IMG WIDTH=58 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline60525" SRC="img613.gif"  > elementof a three-dimensional array <tt>a</tt> like this:<PRE>value = a[i,j,k]</PRE>which invokes the <tt>__getitem__</tt> methodwith the tuple (<I>i</I>,<I>j</I>,<I>k</I>) as the index expression.Similarly,you can modify the  <IMG WIDTH=58 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline60525" SRC="img613.gif"  > element like this:<PRE>a[i,j,k] = value</PRE>which invokes the <tt>setitem</tt> methodwith the tuple (<I>i</I>,<I>j</I>,<I>k</I>) as the index expression.<P>Program&nbsp;<A HREF="page93.html#progmultiDimensionalArrayb"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows howthat <tt>__getitem__</tt> and <tt>__setitem__</tt> methodsare both implemented using a <tt>getOffset</tt> method.The <tt>getOffset</tt> method takes a tuple of <I>n</I> indicesand computes the position of the corresponding elementin the one-dimensional array according to Equation&nbsp;<A HREF="page90.html#eqnfdsaddress"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.This computation takes <I>O</I>(<I>n</I>) time in the worst case,where <I>n</I> is the number of dimensions.Consequently, the running times of the <tt>get</tt> and <tt>set</tt>accessors are also <I>O</I>(<I>n</I>).<P><P><A NAME="3296">&#160;</A><A NAME="progmultiDimensionalArrayb">&#160;</A> <IMG WIDTH=575 HEIGHT=371 ALIGN=BOTTOM ALT="program3167" SRC="img614.gif"  ><BR><STRONG>Program:</STRONG> <tt>MultiDimensionalArray</tt> class 	<tt>__getitem__</tt> and <tt>__setitem__</tt> methods.<BR><P><HR><A NAME="tex2html2282" HREF="page94.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2280" HREF="page89.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2274" HREF="page92.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2284" 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 &#169; 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 + =
减小字号Ctrl + -
显示快捷键?