sort dictionary

mydict={"a":5,"b":1,"c":6};

sorted(mydict,key=mydict.get)
Out[58]: ['b', 'a', 'c']

sorted(mydict,key=mydict.get,reverse=True)
Out[59]: ['c', 'a', 'b']

usage of pickle

import cPickle;
x1=dict();
x2=dict();
x3=dict();
x1={1:"a",2:"b"};
x2={"a":1,"b":2};
x3={"f":1,"m":2};
cPickle.dump((x1,x2,x3),open("test.pkl","wb"));
p,q,r=cPickle.load(open("test.pkl","rb"));
print p;
print q;
print r;

 

posted on 2018-01-02 01:48  finallyly  阅读(179)  评论(0编辑  收藏  举报