使用 Python 在有序字典的开头处插入
如果需要在有序字典的开头处插入元素,可以使用“update”方法。
以下是其演示 −
示例
from collections import OrderedDict
my_ordered_dict = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')])
print("The dictionary is :")
print(my_ordered_dict)
my_ordered_dict.update({'Mark':'7'})
my_ordered_dict.move_to_end('Mark', last = False)
print("The resultant dictionary is : ")
print(my_ordered_dict)输出
The dictionary is :
OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')])
The resultant dictionary is :
OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')])说明
导入必需的包。
使用 OrderedDict’创建有序字典。
在控制台上显示它。
使用“update”方法指定键和值。
使用“move_to_end”方法将键值对移动到末尾。
在控制台上显示输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP