python中實現將普通字典dict轉換爲java中的treeMap

上代碼:java

from heapq import heappush,heappop
from collections import OrderedDict

def toTreeMap(paramMap):
    "將paramMap轉換爲java中的treeMap形式.將map的keys變爲heapq.建立有序字典."
    keys=paramMap.keys()
    heap=[]
    for item in keys:
        heappush(heap,item)

    sort=[]
    while heap:
        sort.append(heappop(heap))

    resMap=OrderedDict()
    for key in sort:
        resMap[key]=paramMap.get(key)

    return resMap
相關文章
相關標籤/搜索