Python的collections模块中的OrderedDict有序字典

排序实现:

 

import collections

 

def numOfRepeat(numOfList,numOfSet):
    d = collections.OrderedDict()
    for i in numOfSet:
        count = 0
        for j in numOfList:
            if i == j:
                count += 1
        print "%d 对应的个数:%d" % (i, count)
        d[i] = count
    # s=sorted(d.items(),key=lambda x:x[0])
    return d
    # s=list(s)
    # for key, value in d.items():
    #     print key, value

笔记记录:

 

Python的collections模块中的OrderedDict有序字典_第1张图片
Python的collections模块中的OrderedDict有序字典_第2张图片

 

常规dict并不跟踪插入顺序,迭代处理会根据键在散列表中存储的顺序来生成值。在OrderDict中则相反,它会记住元素插入的顺序,并在创建迭代器时使用这个顺序。Python的collections模块中的OrderedDict有序字典_第3张图片

 

你可能感兴趣的:(python基础,字典)