📄 node10.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0062)http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Lists</TITLE>
<META content=Lists name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK
href="node10_files/easytut.css" rel=STYLESHEET><LINK href="node11.html"
rel=next><LINK href="node9.html" rel=previous><LINK href="easytut.html"
rel=up><LINK href="node11.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node11.html"
name=tex2html276><IMG align=bottom alt=next border=0 height=24
src="node10_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html272><IMG align=bottom alt=up border=0 height=24
src="node10_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html"
name=tex2html266><IMG align=bottom alt=previous border=0 height=24
src="node10_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html274><IMG align=bottom alt=contents border=0 height=24
src="node10_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node11.html"
name=tex2html277>For Loops</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html273>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html"
name=tex2html267>Defining Functions</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html275>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A
name=CHILD_LINKS><STRONG>Subsections</STRONG></A>
<UL>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html#SECTION001010000000000000000"
name=tex2html278>Variables with more than one value</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html#SECTION001020000000000000000"
name=tex2html279>More features of lists</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html#SECTION001030000000000000000"
name=tex2html280>Examples</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html#SECTION001040000000000000000"
name=tex2html281>Exercises</A> </LI></UL><!--End of Table of Child-Links-->
<HR>
<H1><A name=SECTION001000000000000000000>Lists</A> </H1>
<H1><A name=SECTION001010000000000000000>Variables with more than one value</A>
</H1>You have already seen ordinary variables that store a single value. However
other variable types can hold more than one value. The simplest type is called a
list. Here is a example of a list being used:
<P><PRE>which_one = input("What month (1-12)? ")
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',\
'August', 'September', 'October', 'November', 'December']
if 1 <= which_one <= 12:
print "The month is",months[which_one - 1]
</PRE>
<P>and a output example: <PRE>What month (1-12)? 3
The month is March
</PRE>
<P>In this example the <TT>months</TT> is a list. <TT>months</TT> is defined
with the lines <TT>months = ['January', 'February', 'March', 'April', 'May',
'June', 'July',<CODE>\</CODE> 'August', 'September', 'October', 'November',
'December']</TT> (Note that a <CODE>\</CODE> can be used to split a long line).
The <CODE>[</CODE> and <CODE>]</CODE> start and end the list with comma's
(``<CODE>,</CODE>'') separating the list items. The list is used in
<CODE>months[which_one - 1]</CODE>. A list consists of items that are numbered
starting at 0. In other words if you wanted January you would use
<TT>months[0]</TT>. Give a list a number and it will return the value that is
stored at that location.
<P>The statement <TT>if 1 <= which_one <= 12:</TT> will only be true if
<TT>which_one</TT> is between one and twelve inclusive (in other words it is
what you would expect if you have seen that in algebra).
<P>Lists can be thought of as a series of boxes. For example, the boxes created
by <TT>demolist = ['life',42, 'the universe', 6,'and',7]</TT> would look like
this:
<P>
<TABLE border=1 cellPadding=3>
<TBODY>
<TR>
<TD align=left>box number</TD>
<TD align=middle>0</TD>
<TD align=middle>1</TD>
<TD align=middle>2</TD>
<TD align=middle>3</TD>
<TD align=middle>4</TD>
<TD align=middle>5</TD></TR>
<TR>
<TD align=left>demolist</TD>
<TD align=middle>`life'</TD>
<TD align=middle>42</TD>
<TD align=middle>`the universe'</TD>
<TD align=middle>6</TD>
<TD align=middle>`and'</TD>
<TD align=middle>7</TD></TR></TBODY></TABLE>
<P>Each box is referenced by its number so the statement <TT>demolist[0]</TT>
would get <TT>'life'</TT>, <TT>demolist[1]</TT> would get <TT>42</TT> and so on
up to <TT>demolist[5]</TT> getting <TT>7</TT>.
<P>
<H1><A name=SECTION001020000000000000000>More features of lists</A> </H1>The
next example is just to show a lot of other stuff lists can do (for once I don't
expect you to type it in, but you should probably play around with lists until
you are comfortable with them.). Here goes: <PRE>demolist = ['life',42, 'the universe', 6,'and',7]
print 'demolist = ',demolist
demolist.append('everything')
print "after 'everything' was appended demolist is now:"
print demolist
print 'len(demolist) =', len(demolist)
print 'demolist.index(42) =',demolist.index(42)
print 'demolist[1] =', demolist[1]
#Next we will loop through the list
c = 0
while c < len(demolist):
print 'demolist[',c,']=',demolist[c]
c = c + 1
del demolist[2]
print "After 'the universe' was removed demolist is now:"
print demolist
if 'life' in demolist:
print "'life' was found in demolist"
else:
print "'life' was not found in demolist"
if 'amoeba' in demolist:
print "'amoeba' was found in demolist"
if 'amoeba' not in demolist:
print "'amoeba' was not found in demolist"
demolist.sort()
print 'The sorted demolist is ',demolist
</PRE>
<P>The output is: <PRE>demolist = ['life', 42, 'the universe', 6, 'and', 7]
after 'everything' was appended demolist is now:
['life', 42, 'the universe', 6, 'and', 7, 'everything']
len(demolist) = 7
demolist.index(42) = 1
demolist[1] = 42
demolist[ 0 ]= life
demolist[ 1 ]= 42
demolist[ 2 ]= the universe
demolist[ 3 ]= 6
demolist[ 4 ]= and
demolist[ 5 ]= 7
demolist[ 6 ]= everything
After 'the universe' was removed demolist is now:
['life', 42, 6, 'and', 7, 'everything']
'life' was found in demolist
'amoeba' was not found in demolist
The sorted demolist is [6, 7, 42, 'and', 'everything', 'life']
</PRE>
<P>This example uses a whole bunch of new functions. Notice that you can just
<TT>print</TT> a whole list. Next the <TT>append</TT> function is used to add a
new item to the end of the list. <CODE>len</CODE> returns how many items are in
a list. The valid indexes (as in numbers that can be used inside of the []) of a
list range from 0 to <TT>len - 1</TT>. The <TT>index</TT> function tell where
the first location of an item is located in a list. Notice how
<CODE>demolist.index(42)</CODE> returns 1 and when <CODE>demolist[1]</CODE> is
run it returns 42. The line <CODE>#Next we will loop through the list</CODE> is
a just a reminder to the programmer (also called a comment). Python will ignore
any lines that start with a <CODE>#</CODE>. Next the lines: <PRE>c = 0
while c < len(demolist):
print 'demolist[',c,']=',demolist[c]
c = c + 1
</PRE>Create a variable <TT>c</TT> which starts at 0 and is incremented until it
reaches the last index of the list. Meanwhile the <TT>print</TT> statement
prints out each element of the list.
<P>The <CODE>del</CODE> command can be used to remove a given element in a list.
The next few lines use the <TT>in</TT> operator to test if a element is in or is
not in a list.
<P>The <CODE>sort</CODE> function sorts the list. This is useful if you need a
list in order from smallest number to largest or alphabetical. Note that this
rearranges the list.
<P>In summary for a list the following operations occur:
<P>
<TABLE border=1 cellPadding=3>
<TBODY>
<TR>
<TD align=left>example</TD>
<TD align=left>explanation</TD></TR>
<TR>
<TD align=left><TT>list[2]</TT></TD>
<TD align=left>accesses the element at index 2</TD></TR>
<TR>
<TD align=left><TT>list[2] = 3</TT></TD>
<TD align=left>sets the element at index 2 to be 3</TD></TR>
<TR>
<TD align=left><TT>del list[2] </TT></TD>
<TD align=left>removes the element at index 2</TD></TR>
<TR>
<TD align=left><TT>len(list)</TT></TD>
<TD align=left>returns the length of list</TD></TR>
<TR>
<TD align=left><TT>"value" in list</TT></TD>
<TD align=left>is true if <TT>"value"</TT> is an element in list</TD></TR>
<TR>
<TD align=left><TT>"value" not in list</TT></TD>
<TD align=left>is true if <TT>"value"</TT> is not an element in list</TD></TR>
<TR>
<TD align=left><TT>list.sort()</TT></TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -