page173.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 84 行
HTML
84 行
<HTML><HEAD><TITLE>Finding Items in a List</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="tex2html3202" HREF="page174.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3200" HREF="page170.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3194" HREF="page172.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3204" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION007113000000000000000">Finding Items in a List</A></H3><P>Program <A HREF="page173.html#progorderedListAsArrayc"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines two <tt>OrderedListAsArray</tt> classmethods which search for an object in the ordered list.The <tt>__contains__</tt> method tests whether a particular objectinstance is in the ordered list.The <tt>find</tt> method locates in the listan object which <em>matches</em> its argument.<P><P><A NAME="8856"> </A><A NAME="progorderedListAsArrayc"> </A> <IMG WIDTH=575 HEIGHT=294 ALIGN=BOTTOM ALT="program8692" SRC="img736.gif" ><BR><STRONG>Program:</STRONG> <tt>OrderedListAsArray</tt> class <tt>__contains__</tt> and <tt>find</tt> methods.<BR><P><P>The <tt>__contains__</tt> method is a <tt>bool</tt>-valued method.In addition to <tt>self</tt>,the <tt>__contains__</tt> method takes a single argument, <tt>obj</tt>.This method compares the argument one-by-one withthe contents of the <tt>_array</tt>.Note that this method tests whether <em>a particular object instance</em>is contained in the ordered list using the Python <tt>is</tt> operator.In the worst case, the object sought is not in the list.In this case, the running time of the method is <I>O</I>(<I>n</I>),where <IMG WIDTH=72 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline60691" SRC="img663.gif" > is the number of items in the ordered list.<P>The <tt>find</tt> method also does a search of the ordered list.However, it uses the <tt>==</tt> operator to compare the items.Thus, the <tt>Find</tt> method searches the listfor an object which matches its argument.The <tt>Find</tt> method returns the object found.If no match is found,it returns <tt>None</tt>.The running time of this method depends on the time requiredfor the comparison operator, <IMG WIDTH=53 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline61007" SRC="img737.gif" >.In the worst case, the object sought is not in the list.In this case the running time is <IMG WIDTH=136 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline61009" SRC="img738.gif" >.For simplicity, we will assume that the comparisontakes a constant amount of time.Hence, the running time of the method is also <I>O</I>(<I>n</I>),where <IMG WIDTH=72 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline60691" SRC="img663.gif" > is the number of items in the list.<P>It is important to understand thesubtle distinction between the search done by the<tt>__contains__</tt> method and that done by <tt>find</tt>.The <tt>__contains__</tt> method searches for a specific object instancewhile <tt>find</tt> simply looks for a matching object.Consider the following:<PRE>obj1 = [57]obj2 = [57]list = OrderedListAsArray(1)list.insert(obj1)</PRE>This code fragment creates two <tt>tuple</tt>s,both of which contain a single integer, <tt>57</tt>.Only the first object, <tt>obj1</tt>,is inserted into the ordered list <tt>list</tt>.Consequently, evaluating the expression<PRE>obj1 in list</PRE>results in the value <tt>true</tt>;whereas evaluating the expression<PRE>obj2 in list</PRE>results in the value <tt>false</tt>.<P>On the other hand,if a search is done using the <tt>find</tt> method like this:<PRE>obj3 = list.find(obj2)</PRE>the search will be successful!After the call, <tt>obj3</tt> and <tt>obj1</tt> refer to the same object.<P><HR><A NAME="tex2html3202" HREF="page174.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3200" HREF="page170.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3194" HREF="page172.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3204" 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 + =
减小字号Ctrl + -
显示快捷键?