python序列(列表,元组,字典)的增删改查

列表

操作

列表

方法

示例

增加

list.append(obj)
增加元素到末尾

eg.
>>> list1=['hello','world','how','are','you']
>>> list1.append('!')
>>> list1
['hello', 'world', 'how', 'are', 'you', '!']

list.insert(index, obj)
增加元素到指定位置
index:索引位置
obj:内容

eg.
>>> list1
['hello', 'world', 'how', 'are', 'you', '!']
>>> list1.insert(1,',')
>>> list1
['hello', ',', 'world', 'how', 'are', 'you', '!']

list.extend(list_i)
将list_i列表中的元素增加到list中

eg.
>>> list
['hello', 'how', 'are', 'you']
>>> list.extend(['good','girl'])
>>> list
['hello', 'how', 'are', 'you', 'good', 'girl']

删除

list.pop():
默认删除list末尾的元素
list.pop(index)
删除指定位置的元素,index是索引

eg.
>>> list1
['hello', ',', 'world', 'how', 'are', 'you', '!']
>>> list1.pop()
'!'
>>> list1.pop(1)
','

del list[index]
删除指定位置的元素,index是索引
del list
删除整个列表

eg.
>>> list1
['hello', 'world', 'how', 'are', 'you']
>>> del list1[1]
>>> list1
['hello', 'how', 'are', 'you']

>>> list1
['hello', 'how', 'are', 'you']
>>> del list1
>>> list1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'list1' is not defined

list.remove(obj)
移除列表第一个与obj相等的元素

eg.
>>> list=['hello', 'world', 'how', 'are', 'you']
>>> list.remove('world')
>>> list
['hello', 'how', 'are', 'you']

list.clear()
清空列表全部内容

eg.
>>> list=['hello', 'world', 'how', 'are', 'you']
>>> list.clear()
>>> list
[]

修改

list[index]=obj
修改指定位置的元素

eg.
>>> list1
['hello', 'world', 'how', 'are', 'you']
>>> list1[0]='hi'
>>> list1
['hi', 'world', 'how', 'are', 'you']

查询

list[index]
通过下标索引,从0开始

eg.
>>> list=['hello', 'world', 'how', 'are', 'you']
>>> list[2]
'how'

list[a:b]
切片,顾头不顾尾

eg.
>>> list=['hello', 'world', 'how', 'are', 'you']
>>> list[0:3]
['hello', 'world', 'how']
>>> list[1:]
['world', 'how', 'are', 'you']
>>> list[:3]
['hello', 'world', 'how']
>>> list[:]
['hello', 'world', 'how', 'are', 'you']

元组

操作

元组

方法

示例

增加

tup=tup1+tup2
元组不支持修改,但可以通过连接组合的方式进行增加

eg.
>>> tup1=(1,2,3)
>>> tup2=(4,5,6)
>>> tup=tup1+tup2
>>> tup
(1, 2, 3, 4, 5, 6)

删除

del tup
元组不支持单个元素删除,但可以删除整个元组

eg.
>>> tup
(1, 2, 3, 4, 5, 6)
>>> del tup
>>> tup
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tup' is not defined

修改


tup=tup[index1],tup1[index2], ...
tup=tup[index1:index2]
元组是不可变类型,不能修改元组的元素。可通过现有的字符串拼接构造一个新元组

eg.
>>> tup=('a','b','c','d','e')
>>> tup=tup[1],tup[2],tup[4]
>>> tup
('b', 'c', 'e')

>>> tup=('a','b','c','d','e')
>>> tup=tup[1:3]
>>> tup
('b', 'c')

查询

tup[index]
通过下标索引,从0开始

eg.
>>> tup=(1,2,3,4)
>>> tup[2]
3

tup[a:b]
切片,顾头不顾尾

eg.
>>> tup=(1,2,3,4)
>>> tup[1:3]
(2, 3)
>>> tup[1:]
(2, 3, 4)
>>> tup[:3]
(1, 2, 3)
>>> tup[:]
(1, 2, 3, 4)

字典

操作

字典

方法

示例

增加

dict[key]=value
通过赋值的方法增加元素

eg.
>>> dict={'name':'li','age':1}
>>> dict['class']='first'
>>> dict
{'name': 'li', 'age': 1, 'class': 'first'}

dict.update(dict_i)
把新的字典dict_i的键/值对更新到dict里(适用dict_i中包含与dict不同的key)

eg.
>>> dict={'name': 'li', 'age': 1, 'class': 'first'}
>>> dict.update(school='wawo')
>>> dict
{'name': 'li', 'age': 1, 'class': 'first', 'school': 'wawo'}

删除

del dict[key]
删除单一元素,通过key来指定删除
del dict
删除字典

eg.
>>> dict
{'name': 'li', 'age': 1, 'class': 'first'}
>>> del dict['class']
>>> dict
{'name': 'li', 'age': 1}

>>> dict1={'name': 'li', 'age': 1, 'class': 'first'}
>>> del dict1
>>> dict1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dict1' is not defined

dict.pop(key)
删除单一元素,通过key来指定删除

eg.
>>> dict={'name': 'li', 'age': 1, 'class': 'first'}
>>> dict.pop('name')
'li'
>>> dict
{'age': 1, 'class': 'first'}

dict.clear()
清空全部内容

eg.
>>> dict
{'age': 1, 'class': 'first'}
>>> dict.clear()
>>> dict
{}

修改

dict[key]=value
通过对已有的key重新赋值的方法修改

eg.
>>> dict
{'name': 'pang', 'age': 1, 'class': 'first', 'school': 'wawo'}
>>> dict['name']='li'
>>> dict
{'name': 'li', 'age': 1, 'class': 'first', 'school': 'wawo'}

dict.update(dict_i)
把字典dict_i的键/值对更新到dict里(适用dict_i中包含与dict相同的key)

eg.
>>> dict
{'name': 'li', 'age': 1, 'class': 'first', 'school': 'wawo'}
>>> dict.update(name='pang')
>>> dict
{'name': 'pang', 'age': 1, 'class': 'first', 'school': 'wawo'}

查询

dict[key]
通过key访问value值

eg.
>>> dict={'name': 'pang', 'age': 1, 'class': 'first', 'school': 'wawo'}
>>> dict['name']
'pang'

dict.items()
以列表返回可遍历的(键, 值) 元组数组

eg.
>>> dict={'name': 'pang', 'age': 1, 'class': 'first', 'school': 'wawo'}
>>> dict.items()
dict_items([('name', 'pang'), ('age', 1), ('class', 'first'), ('school', 'wawo')])

dict.keys()
以列表返回一个字典所有键值
dict.values()
以列表返回一个字典所有值

eg.
>>> dict.keys()
dict_keys(['name', 'age', 'class', 'school'])
>>> dict.values()
dict_values(['pang', 1, 'first', 'wawo'])

dict.get(key)
返回指定key的对应字典值,没有返回none

eg.
>>> dict.get('age')
1

猜你喜欢

转载自www.cnblogs.com/lilip/p/9249379.html