Python学习18-列表操作

上一篇 / 下一篇  2014-06-25 17:01:59 / 个人分类:笨办法学Python

$ex39.py 源码
ten_things = "Apples Oranges Crows Telephone Light Sugar"

print ("Wait there's not 10 things in that list, let's fix that.")

stuff = ten_things.split(' ')
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]

while len(stuff) != 10:
    next_one = more_stuff.pop()
    print ("Adding: ", next_one)
    stuff.append(next_one)
    print ("There's %d items now." % len(stuff))

print ("There we go: ", stuff)

print ("Let's do some things with stuff.")

print (stuff[1]) #第二个元素,第一个元素是0号位
print (stuff[-1]) # whoa! fancy #倒数第一个
print (stuff.pop())
print (' '.join(stuff)) # what? cool! #用空格连接stuff列表元素
print ('#'.join(stuff[3:5])) # super stellar! #用#连接stuff列表第四个和第五个元素
 
输出结果:
Wait there's not 10 things in that list, let's fix that.
Adding:  Boy
There's 7 items now.
Adding:  Girl
There's 8 items now.
Adding:  Banana
There's 9 items now.
Adding:  Corn
There's 10 items now.
There we go:  ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Banana', 'Corn']
Let's do some things with stuff.
Oranges
Corn
Corn
Apples Oranges Crows Telephone Light Sugar Boy Girl Banana
Telephone#Light



TAG:

 

评分:0

我来说两句

Open Toolbar