📄 45.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -> Operations on Sequences</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> > <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> > <a href="42.html" class="navtitle">4. Operators and Expressions</a> > <span class="nonavtitle">Operations on Sequences</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="44.html" title="Operations on Numbers"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=45" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="45.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="46.html" title="Operations on Dictionaries"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A55%3A53+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104029061156021238067083</font><a href="read7.asp?bookname=0735710910&snode=45&now=5%2F28%2F2002+8%3A55%3A53+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Operations on Sequences</h3>
<p>The following operators can be applied to sequence types, including strings, lists, and tuples:</p>
<P><TABle boRDER="1" cellSPACing="0" cellpadding="1" width="100%">
<cOlgRoup Span="2">
<tR>
<th vALIGn="top">
<fonT SIZe="2">
<p><b>Operation</b></P>
</FONt></th>
<tH VALign="top">
<font size="2">
<p><b>Description</b></p>
</font></tH>
</tr>
<Tr>
<td ValigN="top">
<fonT SIZe="2">
<p><i><tT CLAss="monofont">s</tt></I>
<TT Class="monofont"> + </TT><I><Tt class="monofont">r</tt></i>
</p>
</font></td>
<td vaLigN="top">
<fonT size="2">
<P>Concatenation</p>
</foNT></TD>
</tr>
<tr>
<TD VAlign="top">
<FONT size="2">
<P><I><TT class="monofont">s</tt></i>
<tt class="monofont"> * </tt><i><tt ClaSs="monofont">n</tt></I>
<tt clAss="monofont">, </tT><I><TT clasS="monofont">n</TT></I>
<tt clASS="monofont"> * </Tt><i><tt CLASs="monofont">s</tt></i>
</p>
</font></td>
<td valign="top">
<fOnt Size="2">
<P>Makes <i><tt cLass="monofont">n</TT></I>
copies of <I><tt clASS="monofont">s</Tt></i>
, where <i><tT CLAss="monofont">n</tt></I>
is an integer</P>
</FOnt></td>
</tr>
<tr>
<td valign="top">
<font SizE="2">
<p><i><tT clasS="monofont">s</tt></i>
<TT CLass="monofont"> % </tT><I><TT clasS="monofont">d</TT></I>
</p>
</fonT></TD>
<Td valign="top">
<font size="2">
<p>String formatting (strings only)</p>
</fonT></td>
</Tr>
<tr>
<Td valIgn="top">
<fONT Size="2">
<p><I><TT Class="monofont">s</TT></I>
<Tt claSS="monofont">[</TT><i><tt class="monofont">i</tt></i>
<tt class="monofont">]</tt></P>
</foNt></td>
<Td valIgn="top">
<fONT Size="2">
<p>Indexing</P>
</FONt></td>
</tR>
<TR>
<Td valIGN="top">
<Font size="2">
<p><i><tt class="monofont">s</tt></i>
<tT clAss="monofont">[</tT><i><tt cLass="monofont">i</TT></I>
<Tt claSS="monofont">:</TT><i><tt cLASS="monofont">j</tt></i>
<tT CLAss="monofont">]</tt></p>
</font></td>
<td valign="top">
<fOnt Size="2">
<P>Slicing</p>
</fonT></td>
</tR>
<TR>
<Td valIGN="top">
<Font sIZE="2">
<P><i><tt cLASS="monofont">x</tt></i>
<tt class="monofont"> in </tt><i><tt class="monofont">s</Tt></i>
<Tt clAss="monofont">, </tt><I><tt cLASS="monofont">x</tt></i>
<tT CLAss="monofont"> not in </tt><I><TT Class="monofont">s</TT></I>
</P>
</font></td>
<td valign="top">
<font siZe="2">
<p>Membership</P>
</fonT></td>
</tr>
<Tr>
<td VALIgn="top">
<foNT SIze="2">
<p><tT CLAss="monofont">for </tt><I><TT Class="monofont">x</tt></i>
<tt class="monofont"> in </tt><i><tt cLasS="monofont">s</tt></i>
<Tt claSs="monofont">:</tt></P>
</FONt></td>
<tD VALign="top">
<fONT Size="2">
<p>Iteration</P>
</FONt></td>
</tr>
<tr>
<td valign="top">
<font sIze="2">
<P><tt cLass="monofont">len(</tT><i><tt CLASs="monofont">s</tt></i>
<TT CLass="monofont">)</tT></P>
</FOnt></td>
<TD VAlign="top">
<font size="2">
<p>Length</p>
</font></td>
</Tr>
<tR>
<td vAlign="top">
<Font SIZE="2">
<p><tt cLASS="monofont">min(</tt><i><tT CLAss="monofont">s</tt></I>
<TT Class="monofont">)</tt></p>
</font></td>
<td valigN="top">
<foNt siZe="2">
<p>Minimum item</p>
</fOnt></tD>
</TR>
<Tr>
<td vALIGn="top">
<fonT SIZe="2">
<p><tt CLASs="monofont">max(</tt><i><tt class="monofont">s</tt></i>
<tt clasS="monofont">)</tt></P>
</fonT></td>
<td ValiGN="top">
<FOnt siZE="2">
<P>Maximum item</P>
</font></TD>
</TR>
</colgROUP>
</table></p>
<p>The <tt class="monofont">+</tt> operator concatenates two sequences of the same type. The <i><tt cLasS="monofont">s</tt></i>
<Tt claSs="monofont"> * </tt><I><TT Class="monofont">n</TT></I>
operator makes <I><tt clASS="monofont">n</Tt></i>
copies of a sequence. However, these are shallow copies that replicate elements by reference only. For example, consider the following code:</p>
<pRE>
a = [3,4,5] # A list
b = [a] # A list containing a
c = 4*b # Make four copies of b
# Now modify a
a[0] == -7
# Look at c
print c </PRe>
<p>The output of this program is the following:</p>
<pre>
[[-7, 4, 5], [-7, 4, 5], [-7, 4, 5], [-7, 4, 5]] </pre>
<p>In this case, a reference to the list <tt class="monofont">a</tt> was placed in the list <tT clAss="monofont">b</tT>. When <tt clAss="monofont">b</tT> was replicated, four additional references to <TT Class="monofont">a</TT> were created. Finally, when <TT clasS="monofont">a</TT> was modified, this change was propagated to all of the other 揷opies
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -