更新Python列表

你可以对列表的数据项进行修改或更新,你也可以使用 append() 方法来添加列表项,如下所示:

实例(Python 3.0+)

#!/usr/bin/python3 list = [‘Google’, ‘python-100’, 1997, 2000] print (“第三个元素为 : “, list[2]) list[2] = 2001 print (“更新后的第三个元素为 : “, list[2]) list1 = [‘Google’, ‘python-100’, ‘Taobao’] list1.append(‘Baidu’) print (“更新后的列表 : “, list1)

注意:我们会在接下来的章节讨论 append() 方法的使用。

以上实例输出结果:

第三个元素为 :  1997
更新后的第三个元素为 :  2001
更新后的列表 :  ['Google', 'python-100', 'Taobao', 'Baidu']
0

评论0

请先

没有账号? 注册  忘记密码?