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

python 示例_带有示例的Python字典popitem()方法相关推荐

  1. python 示例_带有示例的Python字典update()方法

    python 示例 字典update()方法 (Dictionary update() Method) update() method is used to update the dictionary ...

  2. python示例_带有示例的Python功能指南

    python示例 Python函数简介 (Introduction to Functions in Python) A function allows you to define a reusable ...

  3. python 示例_带有示例的Python File write()方法

    python 示例 文件write()方法 (File write() Method) write() method is an inbuilt method in Python, it is use ...

  4. lock_sh 示例_带有示例的Python date __str __()方法

    lock_sh 示例 Python date .__ str __()方法 (Python date.__str__() Method) date.__str__() method is used t ...

  5. python 示例_带有示例的Python文件关闭属性

    python 示例 文件关闭属性 (File closed Property) closed Property is an inbuilt property of File object (IO ob ...

  6. python 示例_带有示例的Python date timetuple()方法

    python 示例 Python date.timetuple()方法 (Python date.timetuple() Method) date.timetuple() method is used ...

  7. python 示例_带有示例的Python date isocalendar()方法

    python 示例 Python date.isocalendar()方法 (Python date.isocalendar() Method) date.isocalendar() method i ...

  8. python 示例_带有示例的Python File close()方法

    python 示例 文件close()方法 (File close() Method) close() method is an inbuilt method in Python, it is use ...

  9. python 示例_带有示例的Python列表remove()方法

    python 示例 列出remove()方法 (List remove() Method) remove() method is used to remove the first occurrence ...

最新文章

  1. Opencv java 二值化函数threshold (10)
  2. 【2015 Week Task】
  3. 拉屎能赚钱?在马桶上月入过万?原来卫生间里还有这么多隐藏福利,超模君都惊了……
  4. [8.21NOIP模拟赛]决战【tarjan】
  5. k66 pit计时功能配置_PIT,JUnit 5和Gradle –仅需额外的一行配置
  6. Ambari架构源码解析
  7. mac设置多个屏幕显示的问题
  8. 怎样在神经网络设计中加入先验信息 - 权值共享
  9. 智慧小区云平台解决方案
  10. 【AAD Connect】05:通过AAD Connect疑难解答检查同步问题,以及根据提示如何解决问题(AD账户迁移到O365)
  11. Chrome流量监控
  12. oracle导入提示字符过长,Oracle 解决【ORA-01704:字符串文字太长】
  13. python输入一个字符、如果是大写字母、转换为小写_python语言 输入一个字母 如果它是一个小写英文字母 则把它转换为对应的大写字母输出?...
  14. (十六)admin-boot项目之文件存储上传与下载minio
  15. 【周志华机器学习】四、决策树
  16. repeater控件 php,asp.net Repeater控件的说明及详细介绍及使用方法
  17. 基于HTML+CSS+JavaScript制作简单的大学生网页设计——我的家乡湖南
  18. 洛谷P1000 超级玛丽游戏C++题解
  19. jieba分词自定义词典
  20. KUKA的Officelite虚拟机运行EthernetKRL3.0成功通讯

热门文章

  1. linux数据包注释,关于 linux中TCP数据包(SKB)序列号的小笔记
  2. node.js中exports与module.exports的区别分析
  3. JAVASCRIPT常用20种小技巧汇总
  4. context元素大概解说
  5. 三、 将DataTable 转换为List
  6. Scala学习笔记-环境搭建以及简单语法
  7. maven2 + tomcat6 + eclipse集成配置
  8. Android 之视频监控
  9. 使用IndexReader.repen提高搜索速度
  10. PostgreSQL 中的递归查询 与oracle 的比较