@Frankchen
2016-03-28T01:28:49.000000Z
字数 2568
阅读 1600
python
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-keydict = {}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 KeyErrorif 'z' in dict: print dict['z'] ## Avoid KeyErrorprint 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 abovefor key in dict.keys(): print key## Get the .keys() list:print dict.keys() ## ['a', 'o', 'g']## Likewise, there's a .values() list of valuesprint dict.values() ## ['alpha', 'omega', 'gamma']## Common case -- loop over the keys in sorted order,## accessing each key/valuefor key in sorted(dict.keys()):print key, dict[key]## .items() is the dict expressed as (key, value) tuplesprint 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,这在数据量很大的时候常用。
%操作符方便的把字典中的value代替为字符串:
hash = {}hash['word'] = 'garfield'hash['count'] = 42s = 'I want %(count)d copies of %(word)s' % hash # %d for int, %s for string# 'I want 42 copies of garfield'
del操作符删除元素,如:
var = 6del var # var no more!list = ['a', 'b', 'c', 'd']del list[0] ## Delete first elementdel list[-2:] ## Delete last two elementsprint list ## ['b']dict = {'a':1, 'b':2, 'c':3}del dict['b'] ## Delete 'b' entryprint dict ## {'a':1, 'c':3}
open()函数打开并且返回一个文件代号,这可以接下来用来读或者写操作。f = open('name','r')的含义是打开一个文件传递给变量f,准备进行读操作,可以用f.close()关闭。还可以使用'w'用来写,'a'用来添加。特殊的'rU'用来将不同的行尾符转化为'\n',for用来遍历文件的每一行很有效,不过注意这只对text文件有效,对二进制文件不起作用。
# Echo the contents of a filef = open('foo.txt', 'rU')for line in f: ## iterates over the lines of the fileprint 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来打印到屏幕。
codecs模块提供对于对于读取Unicode文件的支持。
import codecsf = codecs.open('foo.txt', 'rU', 'utf-8')for line in f:# here line is a *unicode* string