Python Dict 词典的用法

Dict Hash Table

Python's efficient key/value hash table structure is called a "dict". The contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is just an empty pair of curly braces {}.

Looking up or setting a value in a dict uses square brackets, e.g. dict['foo'] looks up the value under the key 'foo'. Strings, numbers, and tuples work as keys, and any type can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use "in" to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present (or get(key, not-found) allows you to specify what value to return in the not-found case).

 
 
  ## 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)

dict with keys 'a' 'o' 'g'

A for loop on a dictionary iterates over its keys by default. The keys will appear in an arbitrary order. The methods dict.keys() and dict.values() return lists of the keys or values explicitly. There's also an items() which returns a list of (key, value) tuples, which is the most efficient way to examine all the key value data in the dictionary. All of these lists can be passed to the sorted() function.

 
 
  ## 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

There are "iter" variants of these methods called iterkeys(), itervalues() and iteritems() which avoid the cost of constructing the whole list -- a performance win if the data is huge. However, I generally prefer the plain keys() and values() methods with their sensible names. In Python 3000 revision, the need for the iterkeys() variants is going away.

Strategy note: from a performance point of view, the dictionary is one of your greatest tools, and you should use where you can as an easy way to organize data. For example, you might read a log file where each line begins with an ip address, and store the data into a dict using the ip address as the key, and the list of lines where it appears as the value. Once you've read in the whole file, you can look up any ip address and instantly see its list of lines. The dictionary takes in scattered data and make it into something coherent.

Dict Formatting

The % operator works conveniently to substitute values from a dict into a string by name:

 
 
  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'

Google Python Class:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值