跟老齊學(xué)Python之有容乃大的list(3)
對(duì)list的操作
向list中插入一個(gè)元素
前面有一個(gè)向list中追加元素的方法,那個(gè)追加是且只能是將新元素添加在list的最后一個(gè)。如:
>>> all_users = ["qiwsir","github"] >>> all_users.append("io") >>> all_users ['qiwsir', 'github', 'io']
從這個(gè)操作,就可以說明list是可以隨時(shí)改變的。這種改變的含義只它的大小即所容納元素的個(gè)數(shù)以及元素內(nèi)容,可以隨時(shí)直接修改,而不用進(jìn)行轉(zhuǎn)換。這和str有著很大的不同。對(duì)于str,就不能進(jìn)行字符的追加。請(qǐng)看官要注意比較,這也是str和list的重要區(qū)別。
與list.append(x)類似,list.insert(i,x)也是對(duì)list元素的增加。只不過是可以在任何位置增加一個(gè)元素。
我特別引導(dǎo)列為看官要通過官方文檔來理解:
list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
這次就不翻譯了。如果看不懂英語,怎么了解貴國(guó)呢?一定要硬著頭皮看英語,不僅能夠?qū)W好程序,更能...(此處省略兩千字)
根據(jù)官方文檔的說明,我們做下面的實(shí)驗(yàn),請(qǐng)看官?gòu)膶?shí)驗(yàn)中理解:
>>> all_users ['qiwsir', 'github', 'io'] >>> all_users.insert("python") #list.insert(i,x),要求有兩個(gè)參數(shù),少了就報(bào)錯(cuò) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: insert() takes exactly 2 arguments (1 given) >>> all_users.insert(0,"python") >>> all_users ['python', 'qiwsir', 'github', 'io'] >>> all_users.insert(1,"http://") >>> all_users ['python', 'http://', 'qiwsir', 'github', 'io'] >>> length = len(all_users) >>> length 5 >>> all_users.insert(length,"algorithm") >>> all_users ['python', 'http://', 'qiwsir', 'github', 'io', 'algorithm']
小結(jié):
list.insert(i,x),將新的元素x 插入到原list中的list[i]前面
如果i==len(list),意思是在后面追加,就等同于list.append(x)
刪除list中的元素
list中的元素,不僅能增加,還能被刪除。刪除list元素的方法有兩個(gè),它們分別是:
list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item. list.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
我這里講授python,有一個(gè)習(xí)慣,就是用學(xué)習(xí)物理的方法。如果看官當(dāng)初物理沒有學(xué)好,那么一定是沒有用這種方法,或者你的老師沒有用這種教學(xué)法。這種方法就是:自己先實(shí)驗(yàn),然后總結(jié)規(guī)律。
先實(shí)驗(yàn)list.remove(x),注意看上面的描述。這是一個(gè)能夠刪除list元素的方法,同時(shí)上面說明告訴我們,如果x沒有在list中,會(huì)報(bào)錯(cuò)。
>>> all_users ['python', 'http://', 'qiwsir', 'github', 'io', 'algorithm'] >>> all_users.remove("http://") >>> all_users #的確是把"http://"刪除了 ['python', 'qiwsir', 'github', 'io', 'algorithm'] >>> all_users.remove("tianchao") #原list中沒有“tianchao”,要?jiǎng)h除,就報(bào)錯(cuò)。 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.remove(x): x not in list
注意兩點(diǎn):
如果正確刪除,不會(huì)有任何反饋。沒有消息就是好消息。
如果所刪除的內(nèi)容不在list中,就報(bào)錯(cuò)。注意閱讀報(bào)錯(cuò)信息:x not in list
看官是不是想到一個(gè)問題?如果能夠在刪除之前,先判斷一下這個(gè)元素是不是在list中,在就刪,不在就不刪,不是更智能嗎?
如果看官想到這里,就是在編程的旅程上一進(jìn)步。python的確讓我們這么做。
>>> all_users ['python', 'qiwsir', 'github', 'io', 'algorithm'] >>> "python" in all_users #這里用in來判斷一個(gè)元素是否在list中,在則返回True,否則返回False True >>> if "python" in all_users: ... all_users.remove("python") ... print all_users ... else: ... print "'python' is not in all_users" ... ['qiwsir', 'github', 'io', 'algorithm'] #刪除了"python"元素 >>> if "python" in all_users: ... all_users.remove("python") ... print all_users ... else: ... print "'python' is not in all_users" ... 'python' is not in all_users #因?yàn)橐呀?jīng)刪除了,所以就沒有了。
上述代碼,就是兩段小程序,我是在交互模式中運(yùn)行的,相當(dāng)于小實(shí)驗(yàn)。
另外一個(gè)刪除list.pop([i])會(huì)怎么樣呢?看看文檔,做做實(shí)驗(yàn)。
>>> all_users ['qiwsir', 'github', 'io', 'algorithm'] >>> all_users.pop() #list.pop([i]),圓括號(hào)里面是[i],表示這個(gè)序號(hào)是可選的 'algorithm' #如果不寫,就如同這個(gè)操作,默認(rèn)刪除最后一個(gè),并且將該結(jié)果返回 >>> all_users ['qiwsir', 'github', 'io'] >>> all_users.pop(1) #指定刪除編號(hào)為1的元素"github" 'github' >>> all_users ['qiwsir', 'io'] >>> all_users.pop() 'io' >>> all_users #只有一個(gè)元素了,該元素編號(hào)是0 ['qiwsir'] >>> all_users.pop(1) #但是非要?jiǎng)h除編號(hào)為1的元素,結(jié)果報(bào)錯(cuò)。注意看報(bào)錯(cuò)信息 Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop index out of range #刪除索引超出范圍,就是1不在list的編號(hào)范圍之內(nèi)
給看官留下一個(gè)思考題,如果要向前面那樣,能不能事先判斷一下要?jiǎng)h除的編號(hào)是不是在list的長(zhǎng)度范圍(用len(list)獲取長(zhǎng)度)以內(nèi)?然后進(jìn)行刪除或者不刪除操作。
list是一個(gè)有意思的東西,內(nèi)涵豐富??磥硐乱恢v還要繼續(xù)講list。并且可能會(huì)做一個(gè)有意思的游戲。請(qǐng)期待。
相關(guān)文章
Python3使用Selenium獲取session和token方法詳解
這篇文章主要介紹了Python3使用Selenium獲取session和token方法詳解,需要的朋友可以參考下2021-02-02python snownlp情感分析簡(jiǎn)易demo(分享)
下面小編就為大家?guī)硪黄猵ython snownlp情感分析簡(jiǎn)易demo(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06Python中內(nèi)建模塊collections如何使用
在本篇內(nèi)容里小編給大家整理的是關(guān)于Python中內(nèi)建模塊collections的用法,有需要的朋友們可以參考下。2020-05-05python執(zhí)行系統(tǒng)命令4種方法與比較
這篇文章主要介紹了python執(zhí)行系統(tǒng)命令4種方法與比較,需要的朋友可以參考下2021-04-04Python3實(shí)現(xiàn)將文件歸檔到zip文件及從zip文件中讀取數(shù)據(jù)的方法
這篇文章主要介紹了Python3實(shí)現(xiàn)將文件歸檔到zip文件及從zip文件中讀取數(shù)據(jù)的方法,涉及Python針對(duì)zip文件操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05python?open函數(shù)中newline參數(shù)實(shí)例詳解
newLine()方法可用于輸出一個(gè)換行字符"/n",下面這篇文章主要給大家介紹了關(guān)于python?open函數(shù)中newline參數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06使用pycharm運(yùn)行flask應(yīng)用程序的詳細(xì)教程
這篇文章主要介紹了使用pycharm運(yùn)行flask應(yīng)用程序,首先大家需要使用pycharm創(chuàng)建你的第一個(gè)app,接下來就開始配置pycharm,需要的朋友可以參考下2021-06-06