📄 node15.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0062)http://www.honors.montana.edu/~jjc/easytut/easytut/node15.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>More on Lists</TITLE>
<META content="More on 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="node15_files/easytut.css" rel=STYLESHEET><LINK href="node16.html"
rel=next><LINK href="node14.html" rel=previous><LINK href="easytut.html"
rel=up><LINK href="node16.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node16.html"
name=tex2html343><IMG align=bottom alt=next border=0 height=24
src="node15_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html339><IMG align=bottom alt=up border=0 height=24
src="node15_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node14.html"
name=tex2html333><IMG align=bottom alt=previous border=0 height=24
src="node15_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html341><IMG align=bottom alt=contents border=0 height=24
src="node15_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node16.html"
name=tex2html344>Revenge of the Strings</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html340>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node14.html"
name=tex2html334>Using Modules</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html342>Contents</A></B> <BR><BR><!--End of Navigation Panel-->
<H1><A name=SECTION001500000000000000000>More on Lists</A> </H1>We have already
seen lists and how they can be used. Now that you have some more background I
will go into more detail about lists. First we will look at more ways to get at
the elements in a list and then we will talk about copying them.
<P>Here are some examples of using indexing to access a single element of an
list: <PRE>>>> list = ['zero','one','two','three','four','five']
>>> list[0]
'zero'
>>> list[4]
'four'
>>> list[5]
'five'
</PRE>All those examples should look familiar to you. If you want the first item
in the list just look at index 0. The second item is index 1 and so on through
the list. However what if you want the last item in the list? One way could be
to use the <CODE>len</CODE> function like <CODE>list[len(list)-1]</CODE>. This
way works since the <CODE>len</CODE> function always returns the last index plus
one. The second from the last would then be <CODE>list[len(list)-2]</CODE>.
There is an easier way to do this. In Python the last item is always index -1.
The second to the last is index -2 and so on. Here are some more examples: <PRE>>>> list[len(list)-1]
'five'
>>> list[len(list)-2]
'four'
>>> list[-1]
'five'
>>> list[-2]
'four'
>>> list[-6]
'zero'
</PRE>Thus any item in the list can be indexed in two ways: from the front and
from the back.
<P>Another useful way to get into parts of lists is using slices. Here is
another example to give you an idea what they can be used for: <PRE>>>> list = [0,'Fred',2,'S.P.A.M.','Stocking',42,"Jack","Jill"]
>>> list[0]
0
>>> list[7]
'Jill'
>>> list[0:8]
[0, 'Fred', 2, 'S.P.A.M.', 'Stocking', 42, 'Jack', 'Jill']
>>> list[2:4]
[2, 'S.P.A.M.']
>>> list[4:7]
['Stocking', 42, 'Jack']
>>> list[1:5]
['Fred', 2, 'S.P.A.M.', 'Stocking']
</PRE>Slices are used to return part of a list. The slice operator is in the
form <CODE>list[first_index:following_index]</CODE>. The slice goes from the
<CODE>first_index</CODE> to the index before the <CODE>following_index</CODE>.
You can use both types of indexing: <PRE>>>> list[-4:-2]
['Stocking', 42]
>>> list[-4]
'Stocking'
>>> list[-4:6]
['Stocking', 42]
</PRE>Another trick with slices is the unspecified index. If the first index is
not specified the beginning of the list is assumed. If the following index is
not specified the whole rest of the list is assumed. Here are some examples: <PRE>>>> list[:2]
[0, 'Fred']
>>> list[-2:]
['Jack', 'Jill']
>>> list[:3]
[0, 'Fred', 2]
>>> list[:-5]
[0, 'Fred', 2]
</PRE>Here is a program example (copy and paste in the poem definition if you
want): <PRE>poem = ["<B>","Jack","and","Jill","</B>","went","up","the","hill","to","<B>",\
"fetch","a","pail","of","</B>","water.","Jack","fell","<B>","down","and",\
"broke","</B>","his","crown","and","<B>","Jill","came","</B>","tumbling",\
"after"]
def get_bolds(list):
true = 1
false = 0
## is_bold tells whether or not the we are currently looking at
## a bold section of text.
is_bold = false
## start_block is the index of the start of either an unbolded
## segment of text or a bolded segment.
start_block = 0
for index in range(len(list)):
##Handle a starting of bold text
if list[index] == "<B>":
if is_bold:
print "Error: Extra Bold"
##print "Not Bold:",list[start_block:index]
is_bold = true
start_block = index+1
##Handle end of bold text
if list[index] == "</B>":
if not is_bold:
print "Error: Extra Close Bold"
print "Bold [",start_block,":",index,"] ",\
list[start_block:index]
is_bold = false
start_block = index+1
get_bolds(poem)
</PRE>with the output being: <PRE>Bold [ 1 : 4 ] ['Jack', 'and', 'Jill']
Bold [ 11 : 15 ] ['fetch', 'a', 'pail', 'of']
Bold [ 20 : 23 ] ['down', 'and', 'broke']
Bold [ 28 : 30 ] ['Jill', 'came']
</PRE>
<P>The <CODE>get_bold</CODE> function takes in a list that is broken into words
and token's. The tokens that it looks for are <CODE><B></CODE> which
starts the bold text and <CODE><\B></CODE> which ends bold text. The
function <CODE>get_bold</CODE> goes through and searches for the start and end
tokens.
<P>The next feature of lists is copying them. If you try something simple like: <PRE>>>> a = [1,2,3]
>>> b = a
>>> print b
[1, 2, 3]
>>> b[1] = 10
>>> print b
[1, 10, 3]
>>> print a
[1, 10, 3]
</PRE>This probably looks surprising since a modification to <TT>b</TT> resulted
in <TT>a</TT> being changed as well. What happened is that the statement <CODE>b
= a</CODE> makes <TT>b</TT> a <EM>reference</EM> to <TT>a</TT>. This means that
<TT>b</TT> can be thought of as another name for <TT>a</TT>. Hence any
modification to <TT>b</TT> changes <TT>a</TT> as well. However some assignments
don't create two names for one list: <PRE>>>> a = [1,2,3]
>>> b = a*2
>>> print a
[1, 2, 3]
>>> print b
[1, 2, 3, 1, 2, 3]
>>> a[1] = 10
>>> print a
[1, 10, 3]
>>> print b
[1, 2, 3, 1, 2, 3]
</PRE>
<P>In this case <TT>b</TT> is not a reference to <TT>a</TT> since the expression
<CODE>a*2</CODE> creates a new list. Then the statement <CODE>b = a*2</CODE>
gives <TT>b</TT> a reference to <CODE>a*2</CODE> rather than a reference to
<TT>a</TT>. All assignment operations create a reference. When you pass a list
as a argument to a function you create a reference as well. Most of the time you
don't have to worry about creating references rather than copies. However when
you need to make modifications to one list without changing another name of the
list you have to make sure that you have actually created a copy.
<P>There are several ways to make a copy of a list. The simplest that works most
of the time is the slice operator since it always makes a new list even if it is
a slice of a whole list: <PRE>>>> a = [1,2,3]
>>> b = a[:]
>>> b[1] = 10
>>> print a
[1, 2, 3]
>>> print b
[1, 10, 3]
</PRE>
<P>Taking the slice <TT>[:]</TT> creates a new copy of the list. However it only
copies the outer list. Any sublist inside is still a references to the sublist
in the original list. Therefore, when the list contains lists the inner lists
have to be copied as well. You could do that manually but Python already
contains a module to do it. You use the <TT>deepcopy</TT> function of the
<TT>copy</TT> module: <PRE>>>> import copy
>>> a = [[1,2,3],[4,5,6]]
>>> b = a[:]
>>> c = copy.deepcopy(a)
>>> b[0][1] = 10
>>> c[1][1] = 12
>>> print a
[[1, 10, 3], [4, 5, 6]]
>>> print b
[[1, 10, 3], [4, 5, 6]]
>>> print c
[[1, 2, 3], [4, 12, 6]]
</PRE>First of all notice that <TT>a</TT> is an array of arrays. Then notice
that when <CODE>b[0][1] = 10</CODE> is run both <TT>a</TT> and <TT>b</TT> are
changed, but <TT>c</TT> is not. This happens because the inner arrays are still
references when the slice operator is used. However with <TT>deepcopy</TT>
<TT>c</TT> was fully copied.
<P>So, should I worry about references every time I use a function or
<CODE>=</CODE>? The good news is that you only have to worry about references
when using dictionaries and lists. Numbers and strings create references when
assigned but every operation on numbers and strings that modifies them creates a
new copy so you can never modify them unexpectedly. You do have to think about
references when you are modifying a list or a dictionary.
<P>By now you are probably wondering why are references used at all? The basic
reason is speed. It is much faster to make a reference to a thousand element
list than to copy all the elements. The other reason is that it allows you to
have a function to modify the inputed list or dictionary. Just remember about
references if you ever have some weird problem with data being changed when it
shouldn't be.
<P>
<HR>
<!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node16.html"
name=tex2html343><IMG align=bottom alt=next border=0 height=24
src="node15_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html339><IMG align=bottom alt=up border=0 height=24
src="node15_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node14.html"
name=tex2html333><IMG align=bottom alt=previous border=0 height=24
src="node15_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html341><IMG align=bottom alt=contents border=0 height=24
src="node15_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node16.html"
name=tex2html344>Revenge of the Strings</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html340>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node14.html"
name=tex2html334>Using Modules</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html342>Contents</A></B> <!--End of Navigation Panel-->
<ADDRESS>Josh Cogliati <A
href="mailto:jjc@honors.montana.edu">jjc@honors.montana.edu</A>
</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -