python 示例_帶有示例的Python字典popitem()方法

python 示例

字典popitem()方法 (Dictionary popitem() Method)

popitem() method is used to remove random/last inserted item from the dictionary.

popitem()方法用于從字典中刪除隨機/最后插入的項目。

Before the Python version 3.7, it removes random item and from version 3.7, it removes last inserted item.

在Python 3.7之前的版本中,它會刪除隨機項,而在3.7版中,它會刪除最后插入的項。

Syntax:

句法:

    dictionary_name.popitem()

Parameter(s):

參數:

  • It does not accept any parameter.

    它不接受任何參數。

Return value:

返回值:

The return type of this method is <class 'tuple'>, it returns the removed item as a tuple (key, value).

此方法的返回類型為<class'tuple'> ,它以元組(鍵,值)的形式返回已刪除的項目。

Example 1:

范例1:

# Python Dictionary popitem() Method with Example
# dictionary declaration
student = {
"roll_no": 101,
"name": "Shivang",
"course": "B.Tech",
"perc" : 98.5
}
# printing dictionary
print("data of student dictionary...")
print(student)
# removing item
x = student.popitem()
print(x, ' is removed.')
# removing item
x = student.popitem()
print(x, ' is removed.')

Output (On Python version 3)

輸出(在Python版本3上)

data of student dictionary...
{'name': 'Shivang', 'course': 'B.Tech', 'perc': 98.5, 'roll_no': 101}
('name', 'Shivang')  is removed.
('course', 'B.Tech')  is removed.

Output (On Python version 3.7.4)

輸出(在Python版本3.7.4上)

data of student dictionary...
{'roll_no': 101, 'name': 'Shivang', 'course': 'B.Tech', 'perc': 98.5}
('perc', 98.5)  is removed.
('course', 'B.Tech')  is removed.

Demonstrate the example, if no more item exists then it returns an error.

演示該示例,如果不存在任何其他項目,則返回錯誤。

Example 2:

范例2:

# Python Dictionary popitem() Method with Example
# dictionary declaration
temp = {
"key1": 1,
"key2": 2
}
# printing dictionary
print("data of temp dictionary...")
print(temp)
# popping item
x = temp.popitem()
print(x, 'is removed.')
# popping item
x = temp.popitem()
print(x, 'is removed.')
# popping item
x = temp.popitem()
print(x, 'is removed.')

Output

輸出量

data of temp dictionary...
{'key2': 2, 'key1': 1}
('key2', 2) is removed.
('key1', 1) is removed.
Traceback (most recent call last):
File "main.py", line 22, in <module>
x = temp.popitem()
KeyError: 'popitem(): dictionary is empty'

翻譯自: https://www.includehelp.com/python/dictionary-popitem-method-with-example.aspx

python 示例

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

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

相關文章

優化算法的意義,之二。

前一篇分析了求質數的兩個算法&#xff0c;在代碼執行效率和系統開銷兩方面進行了比較。 這在通信系統的設計和實現中&#xff0c;是非常重要的兩點。因為需要同時面對的是巨大的用戶群&#xff0c;和復雜的業務應用&#xff0c;通信系統的設計經常要面臨魚與熊掌間的選擇。 用…

srs配置文件分析

配置文件中的每一項都是一個SrsConfDirective對象。 例子&#xff1a;vhost 1、 整個vhost 是一個SrsConfDirective對象。 1.1、名字&#xff1a;std::string name vhost 1.2、參數&#xff1a;std::vectorstd::string args第0個值 defaultVhost 1.3、子SrsConfDirective&a…

寄存器(CPU工作原理)03 - 零基礎入門學習匯編語言08

第二章&#xff1a;寄存器&#xff08;CPU工作原理&#xff09;03 讓編程改變世界 Change the world by program 物理地址 CPU訪問內存單元時要給出內存單元的地址。所有的內存單元構成的存儲空間是一個一維的線性空間。 我們將這個唯一的地址稱為物理地址。 16位結構的CPU…

判別Linux是CentOs還是Ubuntu的最簡單方法

在終端執行以下兩條命令即可 CentOs&#xff1a;yum -help Ubuntu&#xff1a;apt-get -help

threadgroup_Java ThreadGroup toString()方法與示例

threadgroupThreadGroup類的toString()方法 (ThreadGroup Class toString() method) toString() method is available in java.lang package. toString()方法在java.lang包中可用。 toString() method is used to returns string denotation of this thread group (i.e. this m…

240多個jQuery插件

文件上傳(File upload)Ajax File Upload.jQUploader.Multiple File Upload plugin. jQuery File Style.Styling an input type file.Progress Bar Plugin.表單驗證(Form Validation)jQuery Validation.Auto Help.Simple jQuery form validation.jQuery XAV - form validations…

解壓縮命令

.Tar.gz 解壓&#xff1a;Tar zxvf FileName.Tar.gz 壓縮&#xff1a;Tar zcvf FileName.Tar.gz DirName 大致總結了一下Linux下各種格式的壓縮包的壓縮、解壓方法。但是部分方法我沒有用到&#xff0c;也就不全&#xff0c;希望大家幫我補充&#xff0c;我將隨時修改完善&…

Anaconda下安裝OpenCV和Tensorflow(最簡潔高效的方法)

安裝Tensorflow 1&#xff0c;打開Anaconda Navigator 2&#xff0c;手動創建tensorflow環境&#xff0c;這個和你的python版本號一致哈&#xff08;方法一第一步之后&#xff0c;輸入python即可查看當前的版本&#xff09; 3&#xff0c;手動搜索并下載添加 4&#xff0c;…

Java System類console()方法及示例

系統類console()方法 (System class console() method) console() method is available in java.lang package. console()方法在java.lang包中可用。 console() method is used to return the console object which is uniquely associated with the current JVM(Java Virtual …

使用FD_CLOEXEC實現close-on-exec,關閉子進程無用文件描述符

我們經常會碰到需要fork子進程的情況&#xff0c;而且子進程很可能會繼續exec新的程序。這就不得不提到子進程中無用文件描述符的問題&#xff01; fork函數的使用本不是這里討論的話題&#xff0c;但必須提一下的是&#xff1a;子進程以寫時復制&#xff08;COW&#xff0c;C…

mysql_result 對應mysqli哪個_php – 如何在mysqli中轉換mysql_result?

這個代碼以前是在mysql中,現在因為它已被棄用,我決定在mysqli中轉換我的代碼,但是我在我的頁面中有這個問題有分頁,在它使用mysql之前沒有錯誤,但現在我得到了這一行出錯&#xff1a;Warning: mysqli_fetch_assoc() expects exactly 1 parameter, 2 given錯誤是顯而易見的,我知…

Anaconda中下載速度賊慢?

清華鏡像 大致步驟如下&#xff1a; 1&#xff0c;windsR輸入cmd&#xff0c;打開命令終端頁面 輸入conda config --set show_channel_urls yes 2&#xff0c;在相應的C盤目錄下會生成.condarc文件 3&#xff0c;用記事本或者notepad打開進行替換 channels:- defaults show_c…

Delphi面向對象學習隨筆八:物理封裝

作者&#xff1a;巴哈姆特http://www.cnpack.org&#xff08;轉載請注明出去并保持完整&#xff09;前面說過的封裝其實是邏輯意義上的封裝。邏輯封裝是對某一特定邏輯功能模塊的封裝&#xff0c;這個特定邏輯功能塊可以是一個類&#xff0c;當然也可以是一個包&#xff0c;他們…

java 方法 示例_Java掃描器具有示例的NextNextShort()方法

java 方法 示例掃描器類的hasNextShort()方法 (Scanner Class hasNextShort() method) Syntax: 句法&#xff1a; public boolean hasNextShort();public boolean hasNextShort(int rad);hasNextShort() method is available in java.util package. hasNextShort()方法在java.…

mysql創建表時出現1071_mysql 出現1071錯誤怎么辦

mysql 出現1071錯誤的解決辦法&#xff1a;該問題是由鍵值字段長度過長導致。mysql支持數據庫表單一鍵值的最大長度不能超過767字節&#xff0c;超出這個長度即報錯(見標題名稱)。一般情況下&#xff0c;不會有鍵值字段的長度會超出該長度。但是需要注意的是&#xff0c;隨著全…

srs rtmp從監聽到接收到新連接的過程分析

&#xff1a;分析srs從啟動開始的main入口開始。分析rtmp監聽端口&#xff0c;然后到接收到新rtmp連接的過程分析。 目錄一、流程圖二、代碼分析1、srs_main_server.cpp:main()2、srs_main_server.cpp:do_main()3、srs_main_server.cpp:run()4、srs_main_server.cpp:run_master…

OpenCV筆記(基于Python)

學習時&#xff0c;將每塊注釋分別去掉即為一個模塊知識點 圖像數字化 import numpy as np """ #二維的ndarry #兩行四列全是0的uchar類型的二維數組 z np.zeros((2,4),np.uint8) print(type(z)) print(z) print("\n")#兩行四列全是1的整型矩陣 o …

POJ 1222 1681 1830 3185 開關燈問題 (高斯消元 異或方程組)

POJ 1222 EXTENDED LIGHTS OUT 基本的開關燈問題.還保證唯一解. 我們把每一個燈泡當成一個狀態xi&#xff0c;總共有30個&#xff0c;而且每個燈與其他燈的關系也很明顯。所以我們就可以列30方程30個變元的方程組&#xff1a; xi 1 * xi 1 * x(i-1) 1 * x(i1) 1 * x(i-6) …

我看周馬,以及3Q大戰背后的社會問題

如今鬧得不可開交的3Q大戰已經成了一道獨特的風景線&#xff0c;讓我們在茶余飯后又增添了不少談資。這兩個中國最大的客戶端軟件提供商各有擁躉無數&#xff0c;雙方鉚足了勁相互吐口水、扔磚頭&#xff0c;現在貌似到了動刀子了。周、馬在媒體上也都將自己標榜為“美貌與智慧…

Java PushbackReader ready()方法與示例

PushbackReader類ready()方法 (PushbackReader Class ready() method) ready() method is available in java.io package. ready()方法在java.io包中可用。 ready() method is used to check whether this stream is ready to be read or not. ready()方法用于檢查此流是否已準…