Python Dict and File – python字典与文件读写
标签(空格分隔): Python
Dict Hash Table
Python的哈希表结构叫做字典。基本形式为key:value的键值对的集合,被大括号包围。string数字和turple都可以作为key,任何类型都可以作为value。可以使用in
或者dict.get(key)
来确认key是否在字典中。
## Can build up a dict by starting with the the empty dict {}
## and storing key/value pairs into the dict like this:
## dict[key] = value-for-that-key
dict = {}
dict['a'] = 'alpha'
dict['g'] = 'gamma'
dict['o'] = 'omega'
print dict ## {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}
print dict['a'] ## Simple lookup, returns 'alpha'
dict['a'] = 6 ## Put new key/value into dict
'a' in dict ## True
## print dict['z'] ## Throws KeyError
if 'z' in dict: print dict['z'] ## Avoid KeyError
print dict.get('z') ## None (instead of KeyError)
for循环能遍历一个字典的所有的key,而key的顺序是任意的。dict.keys
和dict.values
返回所有的key或者value。还有items()
,它返回一系列的(key, value) tuple,这是最有效的确认字典中所有的键值数据的方法。这些list都可以传递给sorted
函数。
## By default, iterating over a dict iterates over its keys.
## Note that the keys are in a random order.
for key in dict: print key
## prints a g o
## Exactly the same as above
for key in dict.keys(): print key
## Get the .keys() list:
print dict.keys() ## ['a', 'o', 'g']
## Likewise, there's a .values() list of values
print dict.values() ## ['alpha', 'omega', 'gamma']
## Common case -- loop over the keys in sorted order,
## accessing each key/value
for key in sorted(dict.keys()):
print key, dict[key]
## .items() is the dict expressed as (key, value) tuples
print dict.items() ## [('a', 'alpha'), ('o', 'omega'), ('g', 'gamma')]
## This loop syntax accesses the whole dict by looping
## over the .items() tuple list, accessing one (key, value)
## pair on each iteration.
for k, v in dict.items(): print k, '>', v
## a > alpha o > omega g > gamma
有一种变体的iterkeys(), itervalues() , iteritems()
可以避免建造全部的list,这在数据量很大的时候常用。
返回字典中值最大的键值对
temp[vector.keys()[argmax(vector.values())]] = max(vector.values())
字典按值排序
ll = sorted(dic.iteritems(), key=lambda d:d[1])
字典按键值排序
ll = sorted(dic.iteritems(), key=lambda d:d[0])
Dict Formatting
%操作符方便的把字典中的value代替为字符串:
hash = {}
hash['word'] = 'garfield'
hash['count'] = 42
s = 'I want %(count)d copies of %(word)s' % hash # %d for int, %s for string
# 'I want 42 copies of garfield'
A better way to add element to a dict插入字典高效方法
举个例子,我们想统计一些元素的数目,通常来讲,我们可能写出如下的形式:
n = 16
myDict = {}
for i in range(0, n):
char = 'abcd'[i%4]
if char in myDict:
myDict[char] += 1
else:
myDict[char] = 1
print(myDict)
那么当dic很大的时候如下的代码就比上面的高效许多。
n = 16
myDict = {}
for i in range(0, n):
char = 'abcd'[i%4]
try:
myDict[char] += 1
except KeyError:
myDict[char] = 1
print(myDict)
Del删除操作
del
操作符删除元素,如:
var = 6
del var # var no more!
list = ['a', 'b', 'c', 'd']
del list[0] ## Delete first element
del list[-2:] ## Delete last two elements
print list ## ['b']
dict = {'a':1, 'b':2, 'c':3}
del dict['b'] ## Delete 'b' entry
print dict ## {'a':1, 'c':3}
Files
open()
函数打开并且返回一个文件代号,这可以接下来用来读或者写操作。f = open('name','r')
的含义是打开一个文件传递给变量f
,准备进行读操作,可以用f.close()
关闭。还可以使用'w'
用来写,'a'
用来添加。特殊的'rU'
用来将不同的行尾符转化为'\n'
,for
用来遍历文件的每一行很有效,不过注意这只对text文件有效,对二进制文件不起作用。
# Echo the contents of a file
f = open('foo.txt', 'rU')
for line in f: ## iterates over the lines of the file
print line, ## trailing , so print does not add an end-of-line char
## since 'line' already includes the end-of line.
f.close()
每次读一行的操作可以避免使用过多的内存。f.readlines()
method读整个文件加入内存,并且返回一个由每一行组成的list。而f.read()
method读整个文件为一条字符串。
对于写操作来说,f.write()
method是把数据写入一个打开的输出文件的最简单的方法。或者用print >> f, string
来打印到屏幕。
Files Unicode
codecs模块提供对于对于读取Unicode文件的支持。
import codecs
f = codecs.open('foo.txt', 'rU', 'utf-8')
for line in f:
# here line is a *unicode* string