📄 typesseq-mutable.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>2.1.5.4 Mutable Sequence Types </title>
<META NAME="description" CONTENT="2.1.5.4 Mutable Sequence Types ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="previous" href="typesseq-xrange.html" tppabs="http://www.python.org/doc/current/lib/typesseq-xrange.html">
<LINK REL="up" href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">
<LINK REL="next" href="typesmapping.html" tppabs="http://www.python.org/doc/current/lib/typesmapping.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="typesseq-xrange.html" tppabs="http://www.python.org/doc/current/lib/typesseq-xrange.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="typesmapping.html" tppabs="http://www.python.org/doc/current/lib/typesmapping.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="typesseq-xrange.html" tppabs="http://www.python.org/doc/current/lib/typesseq-xrange.html">2.1.5.3 XRange Type</A>
<b class="navlabel">Up:</b> <a class="sectref" href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">2.1.5 Sequence Types</A>
<b class="navlabel">Next:</b> <a class="sectref" href="typesmapping.html" tppabs="http://www.python.org/doc/current/lib/typesmapping.html">2.1.6 Mapping Types</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H3>
<BR>
2.1.5.4 Mutable Sequence Types
</H3>
<P>
List objects support additional operations that allow in-place
modification of the object.
These operations would be supported by other mutable sequence types
(when added to the language) as well.
Strings and tuples are immutable sequence types and such objects cannot
be modified once created.
The following operations are defined on mutable sequence types (where
<var>x</var> is an arbitrary object):
<P>
<table border align="center" style="border-collapse: collapse">
<thead>
<tr>
<th align="center"><b>Operation</b> </th>
<th align="left"><b>Result</b> </th>
<th align="center"><b>Notes</b> </th>
</thead>
<tbody valign='baseline'>
<tr><td align="center" valign="baseline"><code><var>s</var>[<var>i</var>] = <var>x</var></code></td>
<td align="left">item <var>i</var> of <var>s</var> is replaced by <var>x</var></td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code><var>s</var>[<var>i</var>:<var>j</var>] = <var>t</var></code></td>
<td align="left">slice of <var>s</var> from <var>i</var> to <var>j</var> is replaced by <var>t</var></td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code>del <var>s</var>[<var>i</var>:<var>j</var>]</code></td>
<td align="left">same as <code><var>s</var>[<var>i</var>:<var>j</var>] = []</code></td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code><var>s</var>.append(<var>x</var>)</code></td>
<td align="left">same as <code><var>s</var>[len(<var>s</var>):len(<var>s</var>)] = [<var>x</var>]</code></td>
<td align="center">(1)</td>
<tr><td align="center" valign="baseline"><code><var>s</var>.extend(<var>x</var>)</code></td>
<td align="left">same as <code><var>s</var>[len(<var>s</var>):len(<var>s</var>)] = <var>x</var></code></td>
<td align="center">(2)</td>
<tr><td align="center" valign="baseline"><code><var>s</var>.count(<var>x</var>)</code></td>
<td align="left">return number of <var>i</var>'s for which <code><var>s</var>[<var>i</var>] == <var>x</var></code></td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code><var>s</var>.index(<var>x</var>)</code></td>
<td align="left">return smallest <var>i</var> such that <code><var>s</var>[<var>i</var>] == <var>x</var></code></td>
<td align="center">(3)</td>
<tr><td align="center" valign="baseline"><code><var>s</var>.insert(<var>i</var>, <var>x</var>)</code></td>
<td align="left">same as <code><var>s</var>[<var>i</var>:<var>i</var>] = [<var>x</var>]</code>
if <code><var>i</var> >= 0</code></td>
<td align="center"> </td>
<tr><td align="center" valign="baseline"><code><var>s</var>.pop(<big>[</big><var>i</var><big>]</big>)</code></td>
<td align="left">same as <code><var>x</var> = <var>s</var>[<var>i</var>]; del <var>s</var>[<var>i</var>]; return <var>x</var></code></td>
<td align="center">(4)</td>
<tr><td align="center" valign="baseline"><code><var>s</var>.remove(<var>x</var>)</code></td>
<td align="left">same as <code>del <var>s</var>[<var>s</var>.index(<var>x</var>)]</code></td>
<td align="center">(3)</td>
<tr><td align="center" valign="baseline"><code><var>s</var>.reverse()</code></td>
<td align="left">reverses the items of <var>s</var> in place</td>
<td align="center">(5)</td>
<tr><td align="center" valign="baseline"><code><var>s</var>.sort(<big>[</big><var>cmpfunc</var><big>]</big>)</code></td>
<td align="left">sort the items of <var>s</var> in place</td>
<td align="center">(5), (6)</td></tbody>
</table>
Notes:
<DL>
<DT><STRONG>(1)</STRONG></DT>
<DD>The C implementation of Python has historically accepted
multiple parameters and implicitly joined them into a tuple; this
no longer works in Python 2.0. Use of this misfeature has been
deprecated since Python 1.4.
<P>
</DD>
<DT><STRONG>(2)</STRONG></DT>
<DD>Raises an exception when <var>x</var> is not a list object. The
<tt class="method">extend()</tt> method is experimental and not supported by
mutable sequence types other than lists.
<P>
</DD>
<DT><STRONG>(3)</STRONG></DT>
<DD>Raises <tt class="exception">ValueError</tt> when <var>x</var> is not found in
<var>s</var>.
<P>
</DD>
<DT><STRONG>(4)</STRONG></DT>
<DD>The <tt class="method">pop()</tt> method is only supported by the list and
array types. The optional argument <var>i</var> defaults to <code>-1</code>,
so that by default the last item is removed and returned.
<P>
</DD>
<DT><STRONG>(5)</STRONG></DT>
<DD>The <tt class="method">sort()</tt> and <tt class="method">reverse()</tt> methods modify the
list in place for economy of space when sorting or reversing a large
list. They don't return the sorted or reversed list to remind you
of this side effect.
<P>
</DD>
<DT><STRONG>(6)</STRONG></DT>
<DD>The <tt class="method">sort()</tt> method takes an optional argument
specifying a comparison function of two arguments (list items) which
should return <code>-1</code>, <code>0</code> or <code>1</code> depending on whether
the first argument is considered smaller than, equal to, or larger
than the second argument. Note that this slows the sorting process
down considerably; e.g. to sort a list in reverse order it is much
faster to use calls to the methods <tt class="method">sort()</tt> and
<tt class="method">reverse()</tt> than to use the built-in function
<tt class="function">sort()</tt> with a comparison function that reverses the
ordering of the elements.
</DD>
</DL>
<P>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="typesseq-xrange.html" tppabs="http://www.python.org/doc/current/lib/typesseq-xrange.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="typesmapping.html" tppabs="http://www.python.org/doc/current/lib/typesmapping.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="typesseq-xrange.html" tppabs="http://www.python.org/doc/current/lib/typesseq-xrange.html">2.1.5.3 XRange Type</A>
<b class="navlabel">Up:</b> <a class="sectref" href="typesseq.html" tppabs="http://www.python.org/doc/current/lib/typesseq.html">2.1.5 Sequence Types</A>
<b class="navlabel">Next:</b> <a class="sectref" href="typesmapping.html" tppabs="http://www.python.org/doc/current/lib/typesmapping.html">2.1.6 Mapping Types</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -