Python中列表的del,remove和pop函數之間的區別

1)Python del函數 (1) Python del function)

del is nothing but "delete". del is a keyword which basically goes on the position given by the user in del(position) and deletes that element and also changes the positions of all the other elements as it is now not the part of the list.

del只是“刪除”而已。 del是一個關鍵字,它基本上位于用戶在del(position)中給定的位置上,并刪除該元素,并更改所有其他元素的位置,因為它現在不在列表中。

One import thing in delete is that it takes the argument in it is id i.e. not the whole data which is to be deleted only the location of the data.

delete中的一個重要事項是它接受參數中的id,即不是要刪除的整個數據,而是僅數據的位置。

2)Python移除功能 (2) Python remove function)

remove is nothing only the searching of the first occurrence of the element and removes that element.

remove只是搜索元素的第一次出現并刪除該元素。

Note: "remove" is slower than the "del" as it has to search the element which makes it slow.

注意: “刪除”“ del”要慢,因為它必須搜索使其變慢的元素。

3)Python pop函數 (3) Python pop function)

The pop() only takes the single argument i.e the index and removes the element from there without affecting any others position. If we pass the index which was not in the range of the given list then it through the error i.e. "IndexError: pop index out of range".

pop()僅采用單個參數,即索引,并從那里刪除元素,而不會影響其他位置。 如果我們傳遞的索引不在給定列表的范圍內,則它會通過錯誤即“ IndexError:pop index out of range”出現錯誤。

It is not necessary to pass the argument in the pop function if not passed it takes it -1 by itself and remove the element from the last and delete that location from the list.

如果未傳遞參數,則無需在pop函數中傳遞參數,否則參數本身會使其為-1并從最后一個元素中刪除該元素,然后從列表中刪除該位置。

列表中del,remove和pop函數的Python示例 (Python example for del, remove and pop functions in the list)

l=[1,2,3,4,6,5,6,7,8,6] #list 
# del deletes the 4th position element i.e 6  
del(l[4])  
#new list after deletion
print('After deletion:',l)   
#removes the value 6 from list
l.remove(6) 
# new list after removing
print('After removing:',l)    
#pop of the element at location 1 
l.pop(1)    
# new list after pop
print('After pop:',l)     
#pop of the element from the last of the list
l.pop(-3)    
# new list after pop
print('After pop:',l)     
#pop of the elements from the 
l.pop() 
# new list after pop
print('After pop:',l)

Output

輸出量

After deletion: [1, 2, 3, 4, 5, 6, 7, 8, 6]
After removing: [1, 2, 3, 4, 5, 7, 8, 6]
After pop: [1, 3, 4, 5, 7, 8, 6]
After pop: [1, 3, 4, 5, 8, 6]
After pop: [1, 3, 4, 5, 8]

Explanation of the code:

代碼說明:

    In the above code,
del(l[4])
deletes the 4th position element  i.e. 6 of the list,
and also change the position/location of all other further elements.
And, 
l.remove(6)
Removes the element 6 from the list. 
And, while using pop in list
l.pop(1)
Pops off  the first element of the list .
And, 
l.pop(-3)
Pops off the 3rd element from the last 
that means negative value means from last
And,
l.pop( )
If not given any argument by default take that -1.

翻譯自: https://www.includehelp.com/python/difference-between-del-remove-and-pop-functions-of-a-list-in-python.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542804.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542804.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542804.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

lcd圖片轉二進制工具_遼寧2.8寸LCD屏價格,測距儀LCD顯示屏_思邁微

首頁 > 新聞中心發布時間:2020-11-15 08:27:09 導讀:思邁微為您提供遼寧寸LCD屏價格,測距儀LCD顯示屏的相關知識與詳情: 以上便是對LED點陣式顯示屏的軟硬件系統、組成等的技術知識的粗略的介紹。以這款名為《液晶顯示器亮點壞點修復工具》…

python 布爾值 布爾運算 運算符

python基礎語法4布爾值布爾運算運算符算術運算符賦值運算符比較運算符邏輯運算符布爾值 布爾值:Ture(真) Flase(假) 假:Flase、0、’’(空字符串)、[](空列表&#xff0…

python代碼安全性問題_這個python代碼對注入安全嗎?

我在Python中有一個服務器/客戶機套接字對。服務器接收特定的命令,然后準備響應并將其發送到客戶端。在在這個問題中,我關心的只是代碼中的可能的注入:如果可以要求服務器對第二個參數做一些奇怪的事情——如果對命令內容的控制不足以避免不希…

Python | 使用+ =運算符在字符串末尾附加文本

Given a string, and we have to append more string (text) at the end of the string using operator in Python. 給定一個字符串,我們必須在Python中使用 運算符在字符串的末尾附加更多字符串(文本)。 There are two methods to add string (text) at the end …

ps如何修改圖片大小尺寸_PS新手入門教程:學習如何修改畫布的大小

PS新手入門教程:學習如何修改畫布的大小。在photoshop中,可以把畫布理解為一張白紙,而我們要處理的圖像可以理解為這張白紙表面上的畫。我們修改畫布的大小時,圖像并不會隨著畫布的大小而整體變大或縮小,這是修改畫布大…

jfinal poi

2019獨角獸企業重金招聘Python工程師標準>>> 最近項目采用jfinal的項目要對一些excel進行操作,經過考慮采用jfinalpoi,在一些學習,使用后總結并分享一些代碼片段。 導入excel protected Workbook workbook null;protected File filenull;publ…

python 函數 類 模塊

python基礎語法5函數作用域函數參數函數對象無名函數回調函數函數嵌套類類的創建類的調用初始化方法類的繼承類的定制模塊使用模塊函數 封裝好,進行某種功能 del 函數名(參數):函數體return 返回值(可以沒有return語句)作用域 …

activemq nodejs stomp 重連機制_5分鐘優劣分析 Kafka、RabbitMQ、RocketMQ、ActiveMQ消息隊列...

一、資料文檔Kafka:中,有kafka作者自己寫的書,網上資料也有一些。 rabbitmq:有一些不錯的書,網上資料多。 zeromq:少。沒有專門寫zeromq的書,網上的資料多是一些代碼的實現和簡單介紹。 rocketm…

excel保存快捷鍵_干貨 | 快速提高工作效率的電腦快捷鍵!

點擊標題下「藍色微信名」可快速關注隨著科學技術的發展,電腦已經成為每個人生活和工作的必備工具。然而,很多人在使用電腦的過程中非常依賴鼠標,導致雙手需要頻繁離開鍵盤,造成工作間歇中斷,導致用電腦處理工作的效率…

16位的數字高字節和低字節_掩蓋8位數字的較低和較高半字節| 8085微處理器

16位的數字高字節和低字節Problem statement: 問題陳述: To mask lower and higher nibble of 8-bit number using 8085 Microprocessor. 使用8085微處理器掩蓋8位數字的較低和較高半字節。 Algorithm: 算法: Load the content of accumulator A with…

釘釘 ISV 應用開發的一些心得

1. 背景 前段時間從前到后完整地做完了一個簡單的釘釘上的 ISV 應用 —— 猿活動。 最開始想做這么一個小工具,是想到,平時部門中經常會組織一些分享活動,但是這些分享活動卻沒有一個比較直觀的“站點”來記錄一次又一次的,很多人…

mcq 隊列_MCQ | 8086微處理器中的字符串操作指令

mcq 隊列Question 1: 問題1: A string is a collection of characters. Each Character is an of byte length which is stored at successive locations. In the 8086 microprocessor, which characters are considered in the string? 字符串是字符的集合。 每…

python 編碼 解碼 讀寫文件

python基礎語法6編碼解碼encode編碼與decode解碼讀寫文件編碼解碼 計算機是以二進制(0或1)存儲的,以字節為單位,1byte8bit,1KB1024B;1MB1024KB;1GB1024MB 編碼表:ASCII碼&#xff0…

電腦如何設置不休眠_電腦休眠了卻沒法喚醒?設置一下就好!

關注全新【HP惠課廳】,惠普消費新品全知曉逐步復工,辦公室環境又漸漸熟悉了起來午休外出吃飯、忙里偷閑散步、下班不想關電腦……隨手就把電腦休眠了開機也快,網頁和工作內容也不會被關掉休眠功能是挺好用的可有時候,無論怎么點開…

node+bower+gulp+webpack初見

2019獨角獸企業重金招聘Python工程師標準>>> node node模塊管理是通過NPM(即 Node Package Manage,是 NodeJS 模塊管理工具)來處理各模塊之間的依賴。NPM按樹狀結構來管理的,支持某模塊的不同版本。 [前提是本機已安裝…

Java LinkedList公共布爾boolean offerFirst(Object o)方法(帶示例)

LinkedList公共布爾布爾offerFirst(Object o)方法 (LinkedList public boolean offerFirst(Object o) method) This method is available in package java.util.LinkedList.offerFirst(Object o). 軟件包java.util.LinkedList.offerFirst(Object o)中提供了此方法。 This metho…

hbuilder php mysql_xampp本地服務器+HBuilder配置php環境

HBuilder配置PHP環境:下載,運行HBuilder編輯器打開右側小窗口,點擊設置圖標—>設置web服務器—>外置web服務器輸入你想要瀏覽器運行的URL,點擊兩個確定,再重新點擊設置web服務器,選擇PHP類文件(選擇之…

百度地圖手機和電腦不一致_你可能不知道的電腦手機冷知識

各位好久不見呀,自科部科科又回來了!平時我們經常使用電腦,你可能以為你對電腦和手機了如指掌,然而下面的冷知識你知道多少個?01一臺電腦可以有多個桌面如果你熟練的使用多個桌面這個技巧那么就可以躲著家長領導偷懶了…

嵌套字典|python_Python | 如果不是,則使用嵌套,根據銷售額計算折扣

嵌套字典|pythonInput same amount and calculate discount based on the amount and given discount rate in Python. 輸入相同的金額,并根據Python中的金額和給定的折扣率計算折扣。 The discount rates are: 折扣率是: Amount Discount0-5000…

RHEL 7 中 systemctl 的用法(替代service 和 chkconfig)

2019獨角獸企業重金招聘Python工程師標準>>> 1、systemctl是RHEL 7 的服務管理工具中主要的工具,它融合之前service和chkconfig的功能于一體。可以使用它永久性或只在當前會話中啟用/禁用服務。 systemctl可以列出正在運行的服務狀態,如圖&am…