d1={}
d1['a']='A'
d1['b']='B'
d1['c']='C'
d1['d']='D' #此时的d1 = {'a':'A','b':'B','c':'C','d':'D'}
for k,v in d1.items():
print k,v
输出结果为:
a A
c C
b B
d D
由上面的结果可以看出对普通字典进行遍历,输出结果是无序的。
import collections
d1={}
d1=collections.OrderedDict() #将普通字典转换为有序字典
d1['a']='A'
d1['b']='B'
d1['c']='C'
d1['d']='D'
for k,v in d1.items():
print k,v
输出结果为:
a A
b B
c C
d D
对比两种输出结果,不难发现,有序字典可以按字典中元素的插入顺序来输出。
上面两个例子之所以字典中插入元素,而不是一开始就将字典中的元素定义好,是因为有序字典的作用只是记住元素插入顺序并按顺序输出。
如果有序字典中的元素一开始就定义好了,后面没有插入元素这一动作,那么遍历有序字典,其输出结果为空,因为缺少了有序插入这一条件,所以此时有序字典就失去了作用,所以有序字典一般用于动态添加并需要按添加顺序输出的时候。
举例说明:
import collections
d2 = {'a':'A','b':'B','c':'C','d':'D'}
d2=collections.OrderedDict() #将普通字典转换为有序字典
for k,v in d2.items():
print k,v
其输出结果为空。
创建有序字典
import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
dic['k3'] = 'v3'
print(dic)
#输出:OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
clear(清空有序字典)
import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
dic.clear()
print(dic)
#输出:OrderedDict()
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(指定一个列表,把列表中的值作为字典的key,生成一个字典)
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(返回由“键值对组成元素“的列表)
import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
print(dic.items())
#输出:odict_items([('k1', 'v1'), ('k2', 'v2')])
keys(获取字典所有的key)
import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
print(dic.keys())
# 输出:odict_keys(['k1', 'k2'])
move_to_end(指定一个key,把对应的key-value移到最后)
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(获取指定key的value,并在字典中删除)
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(按照后进先出原则,删除最后加入的元素,返回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(获取指定key的value,如果key不存在,则创建)
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(获取字典所有的value,返回一个列表)
import collections
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
dic['k3'] = 'v3'
print(dic.values())
# 输出:odict_values(['v1', 'v2', 'v3'])
以上就是python 有序字典使用方法的详细内容。