20121019 Google python课堂 字典与文件

字典的key可是是 strings,numbers,tuples
in可以判断Dicts中是否有该key
dict.get('a')返回键'a'对于的值,如键'a'不存在,返回None
dict.keys()与dict.values()返回由所有key或values生成的list
dict.items()返回以(key, value)为元的list

 

for k, v in dict.items(): 实现字典的迭代

 

字典的格式输出

1 hash ={'word':'garfield', 'count':42}  

2 s ='I want %(count)d copies of %(word)s'% hash

3 # %d for int, %s for string

4 # 'I want 42 copies of garfield'

Del
删除变量,列表中的元,字典中的元

 

文件 f = open('name', 'a') a表示append,'rU'

 
1 File Unicode

2 import codecs

3 f = codecs.open('foo.txt', 'rU', 'utf-8')

你可能感兴趣的:(python)