Python Dict and File -- python字典与文件读写

来源:互联网 发布:斑马网络 上汽撤资 编辑:程序博客网 时间:2024/06/07 01:10

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-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.keysdict.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,这在数据量很大的时候常用。

返回字典中值最大的键值对

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'] = 42s = '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 = 16myDict = {}for i in range(0, n):    char = 'abcd'[i%4]    if char in myDict:        myDict[char] += 1    else:         myDict[char] = 1print(myDict)

那么当dic很大的时候如下的代码就比上面的高效许多。

n = 16myDict = {}for i in range(0, n):    char = 'abcd'[i%4]    try:        myDict[char] += 1    except KeyError:         myDict[char] = 1print(myDict)

Del删除操作

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}

Files

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来打印到屏幕。

Files Unicode

codecs模块提供对于对于读取Unicode文件的支持。

import codecsf = codecs.open('foo.txt', 'rU', 'utf-8')for line in f:# here line is a *unicode* string
1 0
原创粉丝点击