ten_things="Apples Orange Crows Telephone Light Sugar" # no comma between two words
print "Wait there are not 10 things in that list. Let's fix that."
more_stuff="Day Night Song Frisbee Corn Banana Girl Boy"
#more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
stuff2=more_stuff.split(' ') #注意 别拼写错!!!
stuff=ten_things.split(' ')
while len(stuff)!=10:
next_one=stuff2.pop()
print "Adding:", next_one
stuff.append(next_one)
print "There are %d items now" %len(stuff)
print "There we go: ", stuff
print "Let's do some things with stuff"
print stuff[1]
print stuff[-1] # whoa! fancy
print stuff.pop()
print ' '.join(stuff) # what? cool!
print '#'.join(stuff[3:5]) # super stellar! stuff[3:5] 用法同range(3,5)不包括5
代码细节1:ten_things 是字符串 中间用空格隔开 不是逗号。
2:在原版程序中 more_stuff 以数组形式定义(注释了的那行) 不是字符串,不需要split函数将其分开
列表能做什么:
什么时候不能使用列表:
1.你需要维持秩序的时候。列表只能显示原来的顺序,不能整理(sort)顺序。
2.如果需要通过号码随机访问内容。 记住,这是使用从0开始的基数。
3.如果你需要按顺序遍历内容(第一到最后),使用for循环。