[关闭]
@guoxs 2016-03-20T15:32:28.000000Z 字数 3829 阅读 2839

python 字典与文件

python


字典

key-value

常用操作

字典的特点:
散列表,没有顺序,适合插入、查询操作
key不一定是字符串,但一定是不可变对象

排序

  1. [(k,dict[k]) for k in sorted(dict.keys())] //列表解析,排序结果存到list里
  2. //调用函数
  3. sorted(dict.iteritens().key=lambda d:d[1],
  4. reverse = True
  1. #--------用dict直接生成,
  2. name_age=(('xiaoli',33),('xiaowang',20),('xiaozhang',40))
  3. a=dict(name_age)
  4. a; #{'xiaozhang': 40, 'xiaoli': 33, 'xiaowang': 20}
  5. b=dict(xiaoli=33,xiaowang=20,xiaozhang=40)
  6. b; #{'xiaozhang': 40, 'xiaoli': 33, 'xiaowang': 20}
  7. #--------如何将两个等长度的list合并成dict
  8. text = 'c++ python shell ruby java javascript c'
  9. code_num = [38599, 100931, 26153, 93142, 84275, 184220, 46843]
  10. text_list=text.split(' ')
  11. text_list; #['c++', 'python', 'shell', 'ruby', 'java', 'javascript', 'c']
  12. code_dict = dict(zip(text_list,code_num))
  13. code_dict; #{'c': 46843, 'shell': 26153, 'java': 84275, 'python': 100931, 'javascript': 184220, 'c++': 38599, 'ruby': 93142}
  14. #--------key, keys, items, values
  15. code_dict['python'] #100931
  16. code_dict.keys() #['c', 'shell', 'java', 'python', 'javascript', 'c++', 'ruby']
  17. code_dict.values() #[46843, 26153, 84275, 100931, 184220, 38599, 93142]
  18. code_dict.items()
  19. #[('c', 46843), ('shell', 26153), ('java', 84275), ('python', 100931), ('javascript', 184220), ('c++', 38599), ('ruby', 93142)]
  20. #--------get
  21. a=code_dict.get('fortran',None) #空
  22. a=code_dict.get('fortran','aaa') #aaa
  23. #------- ref and copy
  24. a_ref = code_dict
  25. a_copy = code_dict.copy()
  26. #--------update, del, copy, clear
  27. other_code = {'php':78014,'objective-c':34444}
  28. code_dict.update(other_code)
  29. #{'c': 46843, 'shell': 26153, 'java': 84275, 'python': 100931, 'javascript': 184220, 'c++': 38599, 'objective-c': 34444, 'php': 78014, 'ruby': 93142}
  30. del code_dict['c++']
  31. #{'c': 46843, 'shell': 26153, 'java': 84275, 'python': 100931, 'javascript': 184220, 'objective-c': 34444, 'php': 78014, 'ruby': 93142}
  32. a_ref.clear() #{}
  33. #--------sort key and value
  34. [(k,a_copy[k]) for k in sorted(a_copy.keys())]
  35. #[('c', 46843), ('c++', 38599), ('java', 84275), ('javascript', 184220), ('objective-c', 34444), ('php', 78014), ('python', 100931), ('ruby', 93142), ('shell', 26153)]

文件

常用操作

  1. ########################## file ################
  2. import codecs
  3. f=codecs.open('file_ch.txt','w','utf-8')
  4. f.write(u'hello\n')
  5. f.write(u'world\n')
  6. f.write(u'hello python\n')
  7. f.close()
  8. #read file
  9. f=codecs.open('file_ch.txt','r','utf-8')
  10. print f.readline() #hello
  11. print f.readline() #world
  12. print f.readline() #hello python
  13. f.close()
  14. ########################## os ################
  15. import os
  16. print os.path.exists('file_ch.txt') #true
  17. os.rename('file_ch.txt', 'file_test.txt')
  18. print os.path.exists('file_ch.txt') #false

Shelve库

  1. import shelve
  2. D = shelve.open(file)
  3. D[ 'name' ] = 'xiaoming'
  4. D.close()

pickle/cPickle库

  1. import cPickle
  2. f=open(file,mode)
  3. cPickle.dump(obj,f)
  4. Obj = cPickle.load(f)
  1. ########################## shelve ################
  2. import shelve
  3. f = shelve.open('file_test.shelve')
  4. f['baidu'] = 'www.baidu.com'
  5. f['qq'] = 'www.qq.com'
  6. f['163'] = 'www.163.com'
  7. print f
  8. #{'qq': 'www.qq.com', 'baidu': 'www.baidu.com', '163': 'www.163.com'}
  9. f.close()
  10. g = shelve.open('file_test.shelve')
  11. print g
  12. #{'qq': 'www.qq.com', 'baidu': 'www.baidu.com', '163': 'www.163.com'}
  13. ########################## pickle ################
  14. print "--------------------------------------"
  15. import cPickle
  16. f=open('file_test.pkl','w')
  17. obj1 = 2015,"heibanke",[1,2,3,4],{"python":1990,"java":1992}
  18. obj2 = ['heibanke','junmin11','chutianshu1981','sjtujoe','ygkkv',
  19. 'liuyanping_0904','zhkmxx930']
  20. cPickle.dump(obj1,f)
  21. cPickle.dump(obj2,f)
  22. f.close()
  23. #read file
  24. f=open('file_test.pkl','r')
  25. obj1_r = cPickle.load(f)
  26. print obj1_r
  27. #(2015, 'heibanke', [1, 2, 3, 4], {'python': 1990, 'java': 1992})
  28. obj2_r = cPickle.load(f)
  29. print obj2_r
  30. f.close()
  31. #['heibanke', 'junmin11', 'chutianshu1981', 'sjtujoe', 'ygkkv', 'liuyanping_0904', 'zhkmxx930']
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注