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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM