@guoxs
2016-03-20T07:32:28.000000Z
字数 3829
阅读 3128
python
key-value
常用操作
字典的特点:
散列表,没有顺序,适合插入、查询操作
key不一定是字符串,但一定是不可变对象
排序
[(k,dict[k]) for k in sorted(dict.keys())] //列表解析,排序结果存到list里//调用函数sorted(dict.iteritens().key=lambda d:d[1],reverse = True
#--------用dict直接生成,name_age=(('xiaoli',33),('xiaowang',20),('xiaozhang',40))a=dict(name_age)a; #{'xiaozhang': 40, 'xiaoli': 33, 'xiaowang': 20}b=dict(xiaoli=33,xiaowang=20,xiaozhang=40)b; #{'xiaozhang': 40, 'xiaoli': 33, 'xiaowang': 20}#--------如何将两个等长度的list合并成dicttext = 'c++ python shell ruby java javascript c'code_num = [38599, 100931, 26153, 93142, 84275, 184220, 46843]text_list=text.split(' ')text_list; #['c++', 'python', 'shell', 'ruby', 'java', 'javascript', 'c']code_dict = dict(zip(text_list,code_num))code_dict; #{'c': 46843, 'shell': 26153, 'java': 84275, 'python': 100931, 'javascript': 184220, 'c++': 38599, 'ruby': 93142}#--------key, keys, items, valuescode_dict['python'] #100931code_dict.keys() #['c', 'shell', 'java', 'python', 'javascript', 'c++', 'ruby']code_dict.values() #[46843, 26153, 84275, 100931, 184220, 38599, 93142]code_dict.items()#[('c', 46843), ('shell', 26153), ('java', 84275), ('python', 100931), ('javascript', 184220), ('c++', 38599), ('ruby', 93142)]#--------geta=code_dict.get('fortran',None) #空a=code_dict.get('fortran','aaa') #aaa#------- ref and copya_ref = code_dicta_copy = code_dict.copy()#--------update, del, copy, clearother_code = {'php':78014,'objective-c':34444}code_dict.update(other_code)#{'c': 46843, 'shell': 26153, 'java': 84275, 'python': 100931, 'javascript': 184220, 'c++': 38599, 'objective-c': 34444, 'php': 78014, 'ruby': 93142}del code_dict['c++']#{'c': 46843, 'shell': 26153, 'java': 84275, 'python': 100931, 'javascript': 184220, 'objective-c': 34444, 'php': 78014, 'ruby': 93142}a_ref.clear() #{}#--------sort key and value[(k,a_copy[k]) for k in sorted(a_copy.keys())]#[('c', 46843), ('c++', 38599), ('java', 84275), ('javascript', 184220), ('objective-c', 34444), ('php', 78014), ('python', 100931), ('ruby', 93142), ('shell', 26153)]
常用操作
中文支持
import codecs
f=codecs.open(filename, mode, encoding)
文件操作
import os
os.path.exists(filename)
os.rename(old,new)
########################## file ################import codecsf=codecs.open('file_ch.txt','w','utf-8')f.write(u'hello\n')f.write(u'world\n')f.write(u'hello python\n')f.close()#read filef=codecs.open('file_ch.txt','r','utf-8')print f.readline() #helloprint f.readline() #worldprint f.readline() #hello pythonf.close()########################## os ################import osprint os.path.exists('file_ch.txt') #trueos.rename('file_ch.txt', 'file_test.txt')print os.path.exists('file_ch.txt') #false
Shelve库
import shelveD = shelve.open(file)D[ 'name' ] = 'xiaoming'D.close()
pickle/cPickle库
import cPicklef=open(file,mode)cPickle.dump(obj,f)Obj = cPickle.load(f)
########################## shelve ################import shelvef = shelve.open('file_test.shelve')f['baidu'] = 'www.baidu.com'f['qq'] = 'www.qq.com'f['163'] = 'www.163.com'print f#{'qq': 'www.qq.com', 'baidu': 'www.baidu.com', '163': 'www.163.com'}f.close()g = shelve.open('file_test.shelve')print g#{'qq': 'www.qq.com', 'baidu': 'www.baidu.com', '163': 'www.163.com'}########################## pickle ################print "--------------------------------------"import cPicklef=open('file_test.pkl','w')obj1 = 2015,"heibanke",[1,2,3,4],{"python":1990,"java":1992}obj2 = ['heibanke','junmin11','chutianshu1981','sjtujoe','ygkkv','liuyanping_0904','zhkmxx930']cPickle.dump(obj1,f)cPickle.dump(obj2,f)f.close()#read filef=open('file_test.pkl','r')obj1_r = cPickle.load(f)print obj1_r#(2015, 'heibanke', [1, 2, 3, 4], {'python': 1990, 'java': 1992})obj2_r = cPickle.load(f)print obj2_rf.close()#['heibanke', 'junmin11', 'chutianshu1981', 'sjtujoe', 'ygkkv', 'liuyanping_0904', 'zhkmxx930']