Python列表的一些方法

了解一下 Python列表的一些方法。

① 首先定义一个名字列表,使用 print() BIF在屏幕上显示这个列表。接下来,使用 len() BIF得出列表中有多少个数据项,并访问第二个数据项的值。

② 创建列表之后,可以使用列表方法在列表末尾添加一个数据项(使用 append()方法),或者从列表末尾删除数据(使用 pop()方法),还可以在列表末尾添加一个数据项集合(利用 extend()方法)。

③ 最后,在列表中找到并删除一个特定的数据项(使用 remove()方法),然后在某个特定的位置面前增加一个数据项(使用 insert()方法)。

 1 Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
 2 Type "copyright", "credits" or "license()" for more information.
 3 >>> cast=["Cleese","Palin","Jnoes","Idle"]
 4 >>> print(cast)
 5 ['Cleese', 'Palin', 'Jnoes', 'Idle']
 6 >>> print(len(cast))
 7 4
 8 >>> print (cast [1])
 9 Palin
10 >>> cast.append("Gilliam")
11 >>> print (cast )
12 ['Cleese', 'Palin', 'Jnoes', 'Idle', 'Gilliam']
13 >>> cast.pop()
14 'Gilliam'
15 >>> print (cast )
16 ['Cleese', 'Palin', 'Jnoes', 'Idle']
17 >>> cast.extend (["Gilliam","Chapman"])
18 >>> print (cast )
19 ['Cleese', 'Palin', 'Jnoes', 'Idle', 'Gilliam', 'Chapman']
20 >>> cast.remove ("Chapman")
21 >>> print (cast )
22 ['Cleese', 'Palin', 'Jnoes', 'Idle', 'Gilliam']
23 >>> cast .insert(1,"Chapman")
24 >>> print (cast )
25 ['Cleese', 'Chapman', 'Palin', 'Jnoes', 'Idle', 'Gilliam']
26 >>> 

猜你喜欢

转载自www.cnblogs.com/orangeJJJ/p/9118400.html