您的位置:首页 > 编程语言 > Python开发

python list 操作 增删查

2014-08-15 07:29 393 查看
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']
>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: