码迷,mamicode.com
首页 > 其他好文 > 详细

笨办法38列表操作

时间:2018-01-06 04:39:18      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:教程   ref   oranges   列表操作   计数   some   web   numbers   pytho   

  • 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!

 

笨办法38列表操作

标签:教程   ref   oranges   列表操作   计数   some   web   numbers   pytho   

原文地址:https://www.cnblogs.com/p36606jp/p/8207436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!