home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / m / mutable_se < prev   
Encoding:
Text File  |  1996-11-14  |  2.9 KB  |  45 lines

  1. <TITLE>Mutable Sequence Types -- Python library reference</TITLE>
  2. Prev: <A HREF="../m/more_string_operations" TYPE="Prev">More String Operations</A>  
  3. Up: <A HREF="../s/sequence_types" TYPE="Up">Sequence Types</A>  
  4. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  5. <H3>2.1.5.2. Mutable Sequence Types</H3>
  6. List objects support additional operations that allow in-place
  7. modification of the object.
  8. These operations would be supported by other mutable sequence types
  9. (when added to the language) as well.
  10. Strings and tuples are immutable sequence types and such objects cannot
  11. be modified once created.
  12. The following operations are defined on mutable sequence types (where
  13. <VAR>x</VAR> is an arbitrary object):
  14. <DL>
  15. <DT><I>Operation</I><DD><I>Result</I>  ---  <I>Notes</I>
  16. <P>
  17. <DT><CODE><VAR>s</VAR>[<VAR>i</VAR>] = <VAR>x</VAR></CODE><DD>item <VAR>i</VAR> of <VAR>s</VAR> is replaced by <VAR>x</VAR>
  18. <DT><CODE><VAR>s</VAR>[<VAR>i</VAR>:<VAR>j</VAR>] = <VAR>t</VAR></CODE><DD>slice of <VAR>s</VAR> from <VAR>i</VAR> to <VAR>j</VAR> is replaced by <VAR>t</VAR>
  19. <DT><CODE>del <VAR>s</VAR>[<VAR>i</VAR>:<VAR>j</VAR>]</CODE><DD>same as <CODE><VAR>s</VAR>[<VAR>i</VAR>:<VAR>j</VAR>] = []</CODE>
  20. <DT><CODE><VAR>s</VAR>.append(<VAR>x</VAR>)</CODE><DD>same as <CODE><VAR>s</VAR>[len(<VAR>s</VAR>):len(<VAR>s</VAR>)] = [<VAR>x</VAR>]</CODE>
  21. <DT><CODE><VAR>s</VAR>.count(<VAR>x</VAR>)</CODE><DD>return number of <VAR>i</VAR>'s for which <CODE><VAR>s</VAR>[<VAR>i</VAR>] == <VAR>x</VAR></CODE>
  22. <DT><CODE><VAR>s</VAR>.index(<VAR>x</VAR>)</CODE><DD>return smallest <VAR>i</VAR> such that <CODE><VAR>s</VAR>[<VAR>i</VAR>] == <VAR>x</VAR></CODE>  ---  (1)
  23. <DT><CODE><VAR>s</VAR>.insert(<VAR>i</VAR>, <VAR>x</VAR>)</CODE><DD>same as <CODE><VAR>s</VAR>[<VAR>i</VAR>:<VAR>i</VAR>] = [<VAR>x</VAR>]</CODE>
  24. if <CODE><VAR>i</VAR> >= 0</CODE>
  25. <DT><CODE><VAR>s</VAR>.remove(<VAR>x</VAR>)</CODE><DD>same as <CODE>del <VAR>s</VAR>[<VAR>s</VAR>.index(<VAR>x</VAR>)]</CODE>  ---  (1)
  26. <DT><CODE><VAR>s</VAR>.reverse()</CODE><DD>reverses the items of <VAR>s</VAR> in place
  27. <DT><CODE><VAR>s</VAR>.sort()</CODE><DD>permutes the items of <VAR>s</VAR> to satisfy
  28. <CODE><VAR>s</VAR>[<VAR>i</VAR>] <= <VAR>s</VAR>[<VAR>j</VAR>]</CODE>,
  29. for <CODE><VAR>i</VAR> < <VAR>j</VAR></CODE>  ---  (2)
  30. </DL>
  31.  Notes:
  32. <DL>
  33. <DT><B>(1)</B><DD>Raises an exception when <VAR>x</VAR> is not found in <VAR>s</VAR>.
  34. <P>
  35. <DT><B>(2)</B><DD>The <CODE>sort()</CODE> method takes an optional argument
  36. specifying a comparison function of two arguments (list items) which
  37. should return <CODE>-1</CODE>, <CODE>0</CODE> or <CODE>1</CODE> depending on whether the
  38. first argument is considered smaller than, equal to, or larger than the
  39. second argument.  Note that this slows the sorting process down
  40. considerably; e.g. to sort a list in reverse order it is much faster
  41. to use calls to <CODE>sort()</CODE> and <CODE>reverse()</CODE> than to use
  42. <CODE>sort()</CODE> with a comparison function that reverses the ordering of
  43. the elements.
  44. </DL>
  45.