Python数据基础类型-列表
发布时间:2020-12-20 12:44:01 所属栏目:Python 来源:网络整理
导读:1,列表的创建 list1 = [ ‘ hello ‘ , ‘ world ‘ ,1997,2000 ]list2 = [1,2,3,4,5 ]list3 = [ " a " , " b " , " c " , " d " ]list4 = list() # 创建空列表 list5 = [] # 创建空列表 2,访问列表的值 列表的数据访问需要使用索引序号。 list1 = [‘hell
1,列表的创建list1 = [‘hello‘,‘world‘,1997,2000] list2 = [1,2,3,4,5 ] list3 = ["a","b","c","d"] list4 = list() #创建空列表 list5 = [] #创建空列表 2,访问列表的值列表的数据访问需要使用索引序号。 list1 = [‘hello‘,‘world‘,19,20] list2 = [1,5 ] print "list1[0]: ",list1[0] print "list2[1:5]: ",list2[1:5] 输出结果:
list1[0]: hello
list2[1:5]: [2,5]
3,数值更新列表内容的更新可以直接使用索引序号,进行内容的更新,也可以使用append方法。insert( )在列表的任何位置添加新元素。 list1 = [‘hello‘,20] print list1 list1[0] = "HELLO" print list1 list1.append(first) print list1 运行结果: [‘hello‘,20] [‘HELLO‘,20,‘first‘] 4,列表元素删除列表元素的删除使用del语句,也可以使用remove方法,也可使用pop()方法。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |