Python列表的一些方法
了解一下 Python列表的一些方法。
① 首先定义一个名字列表,使用 print() BIF在屏幕上显示这个列表。接下来,使用 len() BIF得出列表中有多少个数据项,并访问第二个数据项的值。
② 创建列表之后,可以使用列表方法在列表末尾添加一个数据项(使用 append()方法),或者从列表末尾删除数据(使用 pop()方法),还可以在列表末尾添加一个数据项集合(利用 extend()方法)。
③ 最后,在列表中找到并删除一个特定的数据项(使用 remove()方法),然后在某个特定的位置面前增加一个数据项(使用 insert()方法)。
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> cast=["Cleese","Palin","Jnoes","Idle"]
>>> print(cast)
['Cleese', 'Palin', 'Jnoes', 'Idle']
>>> print(len(cast))
4
>>> print (cast [1])
Palin
>>> cast.append("Gilliam")
>>> print (cast )
['Cleese', 'Palin', 'Jnoes', 'Idle', 'Gilliam']
>>> cast.pop()
'Gilliam'
>>> print (cast )
['Cleese', 'Palin', 'Jnoes', 'Idle']
>>> cast.extend (["Gilliam","Chapman"])
>>> print (cast )
['Cleese', 'Palin', 'Jnoes', 'Idle', 'Gilliam', 'Chapman']
>>> cast.remove ("Chapman")
>>> print (cast )
['Cleese', 'Palin', 'Jnoes', 'Idle', 'Gilliam']
>>> cast .insert(1,"Chapman")
>>> print (cast )
['Cleese', 'Chapman', 'Palin', 'Jnoes', 'Idle', 'Gilliam']
>>> 相关推荐
whyname 2020-03-07
alicelmx 2020-01-06
文山羊 2019-12-17
yunfeiyang0 2019-06-12
huzai 2019-09-05
ipqtjmqj 2012-05-04
snowfoxmonitor 2012-11-08
dapeng00 2015-05-24
yongyoumengxiang 2019-06-28
zhoushuntian 2018-01-24
pythonpycharm 2019-04-23
BitTigerio 2018-04-24