全部博文(921)
分类: Python/Ruby
2011-02-12 00:52:34
1、Dictionary语法
>>> phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
>>> phonebook = {'Alice': '2341', 'Alice': '9102', 'Cecil': '3258'} >>> phonebook {'Alice': '9102', 'Cecil': '3258'}
>>> items = [('name', 'Gumby'), ('age', 42)] >>> d = dict(items) >>> d {'age': 42, 'name': 'Gumby'}
>>> d = dict(name='Gumby', age=42) >>> d {'age': 42, 'name': 'Gumby'}
2、基本Dictionary操作
>>> x = {} >>> x[42] = 'Foobar' >>> x {42: 'Foobar'}
3、使用Dictionary格式化String
>>> template = '''%(title)s %(title)s
%(text)s
''' >>> data = {'title': 'My Home Page', 'text': 'Welcome to my home page!'} >>> print template % dataMy Home Page My Home Page
Welcome to my home page!
4、Dictionary方法
(1) clear:移去Dictionary中所有的元素
>>> d = {'age': 42, 'name': 'Gumby'} >>> d {'age': 42, 'name': 'Gumby'} >>> d.clear() >>> d {}
(2) copy:返回具有相同key/value对的新Dictionary(注意value值是共用的)
>>> x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']} >>> y = x.copy() >>> y['username'] = 'mlh' >>> y['machines'].remove('bar') >>> y {'username': 'mlh', 'machines': ['foo', 'baz']} >>> x {'username': 'admin', 'machines': ['foo', 'baz']}
>>> from copy import deepcopy >>> d = {} >>> d['names'] = ['Alfred', 'Bertrand'] >>> c = d.copy() >>> dc = deepcopy(d) >>> d['names'].append('Clive') >>> c {'names': ['Alfred', 'Bertrand', 'Clive']} >>> dc {'names': ['Alfred', 'Bertrand']}
(3) fromkeys:根据给定的key值创建新的Dictionary,缺省value值为None
>>> {}.fromkeys(['name', 'age']) {'age': None, 'name': None}
>>> dict.fromkeys(['name', 'age']) {'age': None, 'name': None}
>>> dict.fromkeys(['name', 'age'], '(unknown)') {'age': '(unknown)', 'name': '(unknown)'}
(4) get:获取项目的value值(不常用)
>>> d = {} >>> print d['name'] Traceback (most recent call last): File "", line 1, in ? KeyError: 'name' >>> print d.get('name') None
>>> d.get('name', 'N/A') 'N/A'
(5) has_key:检查Dictionary中是否有指定key
>>> d = {} >>> d.has_key('name') 0 >>> d['name'] = 'Eric' >>> d.has_key('name') 1
(6) items和iteritems
>>> d = {'title': 'Python Web Site', 'url': '', 'spam': 0} >>> d.items() [('url', ''), ('spam', 0), ('title', 'Python Web Site')]
>>> d.iteritems()>>> list(d.iteritems()) [('url', ''), ('spam', 0), ('title', 'Python Web Site')]
(7) keys和iterkeys
>>> d.keys() ['url', 'spam', 'title'] >>> d.iterkeys()>>> list(d.iterkeys()) ['url', 'spam', 'title']
(8) pop:根据指定key返回value,并从Dictionary中移去该项目:
>>> d = {'x': 1, 'y': 2} >>> d.pop('x') 1 >>> d {'y': 2}
(9) popitem:从Dictionary中随机弹出一个项目返回(Dictionary中的项目是无序的)
>>> d = {'title': 'Python Web Site', 'url': '', 'spam': 0} >>> d.popitem() ('url', '') >>> d {'spam': 0, 'title': 'Python Web Site'}
(10) setdefault:根据指定的key值,对不在Dictionary的项目设置value值
>>> d = {} >>> d.setdefault('name', 'N/A') 'N/A' >>> d {'name': 'N/A'} >>> d['name'] = 'Gumby' >>> d.setdefault('name', 'N/A') 'Gumby' >>> d {'name': 'Gumby'}
(11) update:使用另一个Dictionary的项目来更新指定Dictionary
>>> d = {'title': 'Python Web Site', 'url': '', 'changed': 'Mar 14 22:09:15 MET 2005'} >>> x = {'title': 'Python Language Website'} >>> d.update(x) >>> d {'url': '', 'changed': 'Mar 14 22:09:15 MET 2005', 'title': 'Python Language Website'} (12) values和itervalues >>> d.values() ['', 'Mar 14 22:09:15 MET 2005', 'Python Language Website'] >>> d.itervalues()>>> list(d.itervalues())