Home > Backend Development > Python Tutorial > How to use the python3 OrderedDict class (ordered dictionary)

How to use the python3 OrderedDict class (ordered dictionary)

高洛峰
Release: 2017-03-23 14:56:23
Original
9178 people have browsed it

Create an ordered dictionary

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'dic['k3'] = 'v3'print(dic)#输出:OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
Copy after login

clear(clear the ordered dictionary)

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'dic.clear()print(dic)#输出:OrderedDict()
Copy after login

copy(copy)

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'new_dic = dic.copy()print(new_dic)#输出:OrderedDict([('k1', 'v1'), ('k2', 'v2')])
Copy after login

fromkeys(specify a list and copy the values ​​in the list As the key of the dictionary, generate a dictionary)

import collections
dic = collections.OrderedDict()
name = ['tom','lucy','sam']print(dic.fromkeys(name))print(dic.fromkeys(name,20))#输出:OrderedDict([('tom', None), ('lucy', None), ('sam', None)])#     OrderedDict([('tom', 20), ('lucy', 20), ('sam', 20)])
Copy after login

items(return a list of "key-value pairs")

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'print(dic.items())#输出:odict_items([('k1', 'v1'), ('k2', 'v2')])
Copy after login

keys(get all the keys of the dictionary)

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'print(dic.keys())# 输出:odict_keys(['k1', 'k2'])
Copy after login

move_to_end(Specify a key and move the corresponding key-value to the end)

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'dic['k3'] = 'v3'dic.move_to_end('k1')print(dic)# 输出:OrderedDict([('k2', 'v2'), ('k3', 'v3'), ('k1', 'v1')])
Copy after login

pop(Get the value of the specified key and delete it in the dictionary)

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'dic['k3'] = 'v3'k = dic.pop('k2')print(k,dic)# 输出:v2 OrderedDict([('k1', 'v1'), ('k3', 'v3')])
Copy after login

popitem(Follow the last step) First out principle, delete the last element added and return key-value)

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'dic['k3'] = 'v3'print(dic.popitem(),dic)print(dic.popitem(),dic)# 输出:('k3', 'v3') OrderedDict([('k1', 'v1'), ('k2', 'v2')])#      ('k2', 'v2') OrderedDict([('k1', 'v1')])
Copy after login

setdefault(get the value of the specified key, if the key does not exist, create it)

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'dic['k3'] = 'v3'val = dic.setdefault('k5')print(val,dic)# 输出:None OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k5', None)])
Copy after login

values(get all the dictionary value, returns a list)

import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'dic['k2'] = 'v2'dic['k3'] = 'v3'print(dic.values())# 输出:odict_values(['v1', 'v2', 'v3'])
Copy after login

The above is the detailed content of How to use the python3 OrderedDict class (ordered dictionary). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template