切片list

list有两类常用操作:索引(index)和切片(slice)。

 

昨天我们说的用[]加序号访问的方法就是索引操作。

 

除了指定位置进行索引外,list还可以处理负数的索引。继续用昨天的例子:

 

l = [365, 'everyday', 0.618, True]

 

l[-1]表示l中的最后一个元素。

l[-3]表示倒数第3个元素。

 

切片操作符是在[]内提供一对可选数字,用:分割。冒号前的数表示切片的开始位置,冒号后的数字表示切片到哪里结束。同样,计数从0开始。

注意,开始位置包含在切片中,而结束位置不包括。

 

l[1:3]

 

得到的结果是['everyday', 0.618]。

 

如果不指定第一个数,切片就从列表第一个元素开始。

如果不指定第二个数,就一直到最后一个元素结束。

都不指定,则返回整个列表的一个拷贝。

 

l[:3]

l[1:]

l[:]

 

同索引一样,切片中的数字也可以使用负数。比如:

 

l[1:-1]

 

得到['everyday', 0.618]

 

 

#==== 点球小游戏 ====#

 

昨天有了一次罚球的过程,今天我就让它循环5次,并且记录下得分。先不判断胜负。

 

用score_you表示你的得分,score_com表示电脑得分。开始都为0,每进一球就加1。

 

from random import choice

 

score_you = 0

score_com = 0

direction = ['left', 'center', 'right']

 

for i in range(5):

   print '==== Round %d - You Kick! ====' % (i+1)

   print 'Choose one side to shoot:'

   print 'left, center, right'

   you = raw_input()

   print 'You kicked ' + you

   com = choice(direction)

   print 'Computer saved ' + com

   if you != com:

       print 'Goal!'

       score_you += 1

   else:

       print 'Oops...'

   print 'Score: %d(you) - %d(com)\n' % (score_you, score_com)

 

   print '==== Round %d - You Save! ====' % (i+1)

   print 'Choose one side to save:'

   print 'left, center, right'

   you = raw_input()

   print 'You saved ' + you

   com = choice(direction)

   print 'Computer kicked ' + com

   if you == com:

       print 'Saved!'

   else:

       print 'Oops...'

       score_com += 1

   print 'Score: %d(you) - %d(com)\n' % (score_you, score_com)

 

注意:手机上代码有可能会被换行。

这段代码里有两段相似度很高,想想是不是可以有办法可以用个函数把它们分离出来。

 

 

#==== Crossin的编程教室 ====#

猜你喜欢

转载自jason-long.iteye.com/blog/2376272