EX38
1 ten_things = "Apples Oranges Crows Telephone Light Sugar" 2 3 print "Wait there's not 10 things in that list, let's fix that." 4 5 stuff = ten_things.split(' ') 6 more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"] 7 8 while len(stuff) != 10: 9 next_one = more_stuff.pop() 10 print "Adding: ", next_one 11 stuff.append(next_one) 12 print "There's %d items now." % len(stuff) 13 14 print "There we go: ", stuff 15 16 print "Let's do some things with stuff." 17 18 print stuff[1] 19 print stuff[-1] # whoa! fancy 20 print stuff.pop() 21 print ' '.join(stuff) # what? cool! 22 print '#'.join(stuff[3:5]) # super stellar!
行5:用split对ten_things进行(' ')切片
str.split(str="", num=string.count(str)).
行8:利用while一直循环,直到出现flase原理,stuff字串数量不等于10
行9:pop()对more_stuff移除列表中一个元素(默认最后一个元素),并打印出来
list.pop(obj=list[-1])
行11:在列表末尾添加新的对象
list.append(obj)
行21:join():将序列中的元素以指定的字符连接生成一个新的字符串
str.join(sequence)
sequence -- 要连接的元素序列
TIPE:了解行5,8-12的运行逻辑
# 去阅读“面向对象编程”(Object Oriented Programming, OOP)的资料