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

How to use the python3 OrderedDict class (ordered dictionary)

高洛峰
高洛峰Original
2017-03-23 14:56:239009browse

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')])

clear(clear the ordered dictionary)

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

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')])

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)])

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')])

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'])

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')])

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')])

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')])

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)])

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'])

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!

Statement:
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