日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2023/12/1 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 示例_带有示例的Python字典popitem()方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python 示例

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

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

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

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

在Python 3.7之前的版本中,它會(huì)刪除隨機(jī)項(xiàng),而在3.7版中,它會(huì)刪除最后插入的項(xiàng)。

Syntax:

句法:

dictionary_name.popitem()

Parameter(s):

參數(shù):

  • It does not accept any parameter.

    它不接受任何參數(shù)。

Return value:

返回值:

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

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

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.

演示該示例,如果不存在任何其他項(xiàng)目,則返回錯(cuò)誤。

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 示例

總結(jié)

以上是生活随笔為你收集整理的python 示例_带有示例的Python字典popitem()方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。