列表的操作,习题38,learn python the hard way.

]```
#coding=utf-8
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(' ') #str.split(' ', num)  num为分割次数。split()将字符串分割并组成列表。
more_stuff = ["Day", "Night", "Song", "Friebee", "Corn", "Banana", "Girl", "Boy"]

#循环,
while len(stuff) != 10:
    next_one = more_stuff.pop()  #在循环中,列表more_stuff中的元素将被一个一个移除并赋值给变量next_one, next_one会被追加进stuff
    print "Adding:", next_one
    stuff.append(next_one)
    print "There's %d items now." % len(stuff)  #len计算的是列表中元素的个数

print "There we go:", stuff

print "Let's do something with stuff"

print stuff[1] #打印列表位置1上的元素
print stuff[-1] #0是列表第一个元素,同理可得:-1的位置是在列表中倒数第一个。
print stuff.pop() #打印列表最后一位元素,同时该元素移除出列表
print ' '.join(stuff) #在整个列表元素之间插入空格符
print '#'.join(stuff[3:5])  #为指定列表位置上元素之间插入符号#

“`

你可能感兴趣的:(python)