- split(python基础教程p52)
非常重要的字符串方法,是join的逆方法,用来将字符串分割成序列;
- len(python基础教程p33)
内建函数len,返回序列中包含元素的数量;
- pop(python基础教程p38)
pop方法,移除列表中的最后(默认)一个元素,并返回该元素的值,用法如下:
>>> x = [1, 2, 3] >>> x.pop() 3 >>> x [1, 2] >>> x.pop(0) 1 >>> x [2]
- append(python基础教程p36)
append方法用于在列表末尾追加新对象;
- 分片(python基础教程p29)
访问一定范围内的元素,前一个索引的元素包含在分片内,后一个不包含在分片内
>>> numbers = [1, 2, 3, 4, 5, 6] >>> numbers[2,5] [3, 4, 5] >>> numbers[0, 1] [1]
>>> tag = ‘<a href="http://www.python.org">Python web site</a>‘
>>> tag[9:30]
‘http://www.python.org‘
>>> tag[32:-4]
‘Python web site‘
(从“<”开始计数,即“<”是第0个元素)
以下为代码,加了一些print("test",xxxxxxxx)方便检查
1 ten_things = "Apples Oranges Crows Telephone Light Sugar" 2 3 print("Wait there are not 10 things in that list. Let‘s fix that.") 4 5 stuff = ten_things.split(‘ ‘) 6 # print("test", stuff) 7 8 more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"] 9 10 while len(stuff) != 10: 11 next_one = more_stuff.pop() 12 # print("test", more_stuff) 13 print("Adding:", next_one) 14 stuff.append(next_one) 15 # print("test",stuff) 16 print("There are %d items now." % len(stuff)) 17 18 print("There we go:", stuff) 19 20 print("Let‘s do some things with stuff.") 21 22 print(stuff[1]) 23 print(stuff[-1]) # whoa! fancy 24 print(stuff.pop()) 25 # print("test", stuff) 26 print(‘ ‘.join(stuff)) # what? cool! 27 # print("test", stuff[3:5]) 28 print(‘#‘.join(stuff[3:5])) #super stellar!