Python3學(xué)習(xí)筆記之列表方法示例詳解
前言
本文主要給大家介紹了關(guān)于Python3列表方法的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
1 使用[]或者list()創(chuàng)建列表
user = [] user = list()
2 使用list() 可以將其他類型轉(zhuǎn)換成列表
# 將字符串轉(zhuǎn)成列表
>>> list('abcde')
['a', 'b', 'c', 'd', 'e']
# 將元祖轉(zhuǎn)成列表
>>> list(('a','b','c'))
['a', 'b', 'c']
3 使用[offset]獲取元素 或 修改元素
>>> users = ['a','b','c','d','e'] # 可以使用整數(shù)來獲取某個元素 >>> users[0] 'a' # 可以使用負(fù)整數(shù)來表示從尾部獲取某個元素 >>> users[-1] 'e' # 數(shù)組越界會報錯 >>> users[100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> users[-100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range # 修改某個元素 >>> users[0] = 'wdd' >>> users ['wdd', 'b', 'c', 'd', 'e'] >>>
4 列表切片與提取元素
列表的切片或者提取之后仍然是一個列表
形式如:list[start:end:step]
>>> users ['wdd', 'b', 'c', 'd', 'e'] # 正常截取 注意這里并不會截取到users[2] >>> users[0:2] ['wdd', 'b'] # 也可從尾部截取 >>> users[0:-2] ['wdd', 'b', 'c'] # 這樣可以獲取所有的元素 >>> users[:] ['wdd', 'b', 'c', 'd', 'e'] # 也可以加上步長參數(shù) >>> users[0:4:2] ['wdd', 'c'] # 也可以通過這種方式去將列表取反 >>> users[::-1] ['e', 'd', 'c', 'b', 'wdd'] # 注意切片時,偏移量可以越界,越界之后不會報錯,仍然按照界限來處理 例如開始偏移量如果小于0,那么仍然會按照0去計算。 >>> users ['wdd', 'b', 'c', 'd', 'e'] >>> users[-100:3] ['wdd', 'b', 'c'] >>> users[-100:100] ['wdd', 'b', 'c', 'd', 'e'] >>>
5 使用append()添加元素至尾部
形式如:list.append(item)
>>> users
['wdd', 'b', 'c', 'd', 'e']
>>> users.append('ddw')
>>> users
['wdd', 'b', 'c', 'd', 'e', 'ddw']
6 使用extend()或+=合并列表
形式如:list1.extend(list2)
這兩個方法都會直接修改原列表
>>> users ['wdd', 'b', 'c', 'd', 'e', 'ddw'] >>> names ['heihei', 'haha'] >>> users.extend(names) >>> users ['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha'] >>> users += names >>> users ['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha']
7 使用insert()在指定位置插入元素
形式如:list.insert(offset, item)
insert也不存在越界的問題,偏移量正負(fù)都行,越界之后會自動伸縮到界限之內(nèi),并不會報錯
>>> users ['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha'] >>> users.insert(0,'xiaoxiao') >>> users ['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha'] >>> users.insert(-1,'-xiaoxiao') >>> users ['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha'] # 下面-100肯定越界了 >>> users.insert(-100,'-xiaoxiao') >>> users ['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha'] # 下面100也是越界了 >>> users.insert(100,'-xiaoxiao') >>> users ['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
8 使用del刪除某個元素
形式如:del list[offset]
del是python的語句,而不是列表的方法,del刪除不存在的元素時,也會提示越界
>>> users ['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao'] >>> del users[0] >>> users ['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao'] >>> del users[100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range >>> del users[-100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range
9 使用remove刪除具有指定值的元素
形式如:list.remove(value)
>>> users
['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
# 刪除指定值'c'
>>> users.remove('c')
>>> users
['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
# 刪除不存在的值會報錯
>>> users.remove('alsdkfjalsdf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
# 如果該值存在多個,那么只能刪除到第一個
>>> users.remove('haha')
>>> users
['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
10 使用pop()方式返回某個元素后,并在數(shù)組里刪除它
形式如:list.pop(offset=-1) 偏移量默認(rèn)等于-1,也就是刪除最后的元素
>>> users ['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao'] # 刪除最后的元素 >>> users.pop() '-xiaoxiao' >>> users ['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha'] # 如果列表本身就是空的,那么pop時會報錯 >>> user.pop(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop from empty list >>> users.pop(0) 'xiaoxiao' >>> users ['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha'] # 越界時也會報錯 >>> users.pop(100) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop index out of range
11 使用index()查詢具有特定值的元素位置
形式如:list.index(value)
# index只會返回第一遇到該值得位置
>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
>>> users.index('heihei')
5
# 如果該值不存在,也會報錯
>>> users.index('laksdf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'laksdf' is not in list
12 使用in判斷值是否存在列表
形式如:value in list
>>> users ['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha'] >>> 'wdd' in users True
13 使用count()記錄特定值出現(xiàn)的次數(shù)
形式如:list.count(value)
>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
>>> users.count('heihei')
2
>>> users.count('h')
0
14 使用join()將列表轉(zhuǎn)為字符串
形式如:string.join(list)
>>> users ['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha'] >>> ','.join(users) 'wdd,b,d,e,ddw,heihei,heihei,-xiaoxiao,haha' >>> user [] >>> ','.join(user) ''
15 使用sort()重新排列列表元素
形式如:list.sort()
>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
# 默認(rèn)是升序排序
>>> users.sort()
>>> users
['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
# 加入reverse=True, 可以降序排序
>>> users.sort(reverse=True)
>>> users
['wdd', 'heihei', 'heihei', 'haha', 'e', 'ddw', 'd', 'b', '-xiaoxiao']
# 通過匿名函數(shù),傳入函數(shù)進(jìn)行自定義排序
>>> students
[{'name': 'wdd', 'age': 343}, {'name': 'ddw', 'age': 43}, {'name': 'jik', 'age': 90}]
>>> students.sort(key=lambda item: item['age'])
>>> students
[{'name': 'ddw', 'age': 43}, {'name': 'jik', 'age': 90}, {'name': 'wdd', 'age': 343}]
>>> students.sort(key=lambda item: item['age'], reverse=True)
>>> students
[{'name': 'wdd', 'age': 343}, {'name': 'jik', 'age': 90}, {'name': 'ddw', 'age': 43}]
>>>
16 使用reverse()將列表翻轉(zhuǎn)
形式如:list.reverse()
>>> users ['wdd', 'heihei', 'heihei', 'haha', 'e', 'ddw', 'd', 'b', '-xiaoxiao'] >>> users.reverse() >>> users ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
17 使用copy()復(fù)制列表
形式如:list2 = list1.copy()
list2 = list1 這種并不是列表的復(fù)制,只是給列表起了別名。實(shí)際上還是指向同一個值。
>>> users ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd'] >>> users2 = users.copy() >>> users2 ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd'] >>>
18 使用clear()清空列表
形式如: list.clear()
>>> users2 ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd'] >>> users2.clear() >>> users2 []
19 使用len()獲取列表長度
形式如:len(list)
>>> users ['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd'] >>> len(users) 9
20 關(guān)于列表越界的深入思考
寫了這些方法后,我有一些疑問,為什么有些操作會提示越界,有些則不會呢?
會提示偏移量越界的操作有
list[offset]讀取或者修改某個元素del list[offset]刪除指定位置的元素list.remove(value)刪除指定值的元素list.pop(offset)刪除指定位置的元素
如果偏移量越界,這些方法會報錯的。我的個人理解是:
假如我想讀取偏移量為10的元素,但是該元素并不存在,如果系統(tǒng)自動給你讀取了列表的最后一個元素,而且不報錯,這是無法容忍的bug。 如果我想刪除第10個元素,但是第10個元素并不存在,而系統(tǒng)幫你刪除了列表的最后一個元素,我覺得這也是無法容忍的。
所以在使用這些方法時,務(wù)必確認(rèn)該偏移量的元素是否存,否則可能會報錯。
總結(jié)
以上就是這篇文章的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Python3 把一個列表按指定數(shù)目分成多個列表的方式
- Python3 列表,數(shù)組,矩陣的相互轉(zhuǎn)換的方法示例
- Python3列表內(nèi)置方法大全及示例代碼小結(jié)
- Python3中列表list合并的四種方法
- Python3中的列表生成式、生成器與迭代器實(shí)例詳解
- Python3實(shí)現(xiàn)的字典、列表和json對象互轉(zhuǎn)功能示例
- python3 判斷列表是一個空列表的方法
- Python3中的列表,元組,字典,字符串相關(guān)知識小結(jié)
- Python3基礎(chǔ)之list列表實(shí)例解析
- Python3列表List入門知識附實(shí)例
相關(guān)文章
探究數(shù)組排序提升Python程序的循環(huán)的運(yùn)行效率的原因
這篇文章主要介紹了探究數(shù)組排序提升Python程序的循環(huán)的運(yùn)行效率的原因,作者用代碼實(shí)踐了多個小片段來進(jìn)行對比解釋,需要的朋友可以參考下2015-04-04
Pytorch使用卷積神經(jīng)網(wǎng)絡(luò)對CIFAR10圖片進(jìn)行分類方式
這篇文章主要介紹了Pytorch使用卷積神經(jīng)網(wǎng)絡(luò)對CIFAR10圖片進(jìn)行分類方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
用Python自動清理系統(tǒng)垃圾的實(shí)現(xiàn)
這篇文章主要介紹了用Python自動清理系統(tǒng)垃圾的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
跟老齊學(xué)Python之大話題小函數(shù)(2)
上篇文章我們講訴了map 和lambda函數(shù)的使用,本文我們繼續(xù)來看看reduce和filter函數(shù),有需要的朋友可以參考下2014-10-10
Python必備shelve與dbm本地持久化存儲數(shù)據(jù)的兩個強(qiáng)大工具
當(dāng)涉及存儲大量數(shù)據(jù)并且需要高效訪問時,shelve和dbm模塊是Python中用于本地持久化存儲數(shù)據(jù)的兩個強(qiáng)大工具,它們允許開發(fā)人員以鍵值對的形式存儲數(shù)據(jù),并支持快速的檢索和更新操作,在本文將深入探討這兩個模塊,展示它們的優(yōu)勢和應(yīng)用場景2024-01-01
Python Websocket服務(wù)端通信的使用示例
這篇文章主要介紹了Python Websocket服務(wù)端通信的使用示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

