python list 操作 增删查

来源:互联网 发布:帝国cms免费吗 编辑:程序博客网 时间:2024/05/16 12:55
List查询数组里面的元素直接   数组名称.[数组序号]就可以了例如数组b, b[2]<span style="font-size:18px;"><strong>增加元素有3种方式</strong></span>1.append() 增加在最后面例如<pre name="code" class="python">b.append('world')

2.insert()可以进行选择插入到第几个
例如:
>>> b.insert(0,'xiao')
3.extend() 增加多个元素
例如
>>> b.extend(['by','pragram'])
删除元素有3种方式

remove() 直接移除list中的字符串
例如
b.remove('xiao')

del 直接移除list序号的字符串
例如
del b[2]

pop()
例如

b.pop(2)
默认是最后一个
>>> b=['change','the']>>> b.append('world')>>> b['change', 'the', 'world']>>> b.extend(['by','pragram'])>>> b['change', 'the', 'world', 'by', 'pragram']>>> b.insert(0,'xiao')>>> b['xiao', 'change', 'the', 'world', 'by', 'pragram']>>> b[2]'the'>>> b.remove('xiao')>>> b['change', 'the', 'world', 'by', 'pragram']>>> del b(1)SyntaxError: can't delete function call>>> del b[1]>>> b['change', 'world', 'by', 'pragram']>>> a=b;>>> b.pop()'pragram'>>> b['change', 'world', 'by']>>> name=b.pop()>>> name'by'>>> name=b.pop(1)>>> name'world'>>> b['change']>>>



0 0
原创粉丝点击