Python<字典>

来源:互联网 发布:招聘程序员的网站 编辑:程序博客网 时间:2024/05/22 00:06
  • 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
    d = {key1 : value1, key2 : value2 }

  • 键必须是唯一的,但值则不必

>>> diction={"a":2,"a":5}>>> diction{'a': 5}
  • 访问字典中的元素
In [1]: dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [2]: dictOut[2]: {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [3]: dict['Alice']Out[3]: '2341'
  • 修改字典的值
In [1]: dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [2]: dictOut[2]: {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [3]: dict['Alice']Out[3]: '2341'
  • 删除字典
In [1]: dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [2]: dictOut[2]: {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [3]: dict['Alice']Out[3]: '2341'
  • cmp(dict1, dict2)比较两个字典有多少个不同的键值对
In [14]: dict1 = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [15]: dictOut[15]: {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [16]: cmp(dict,dict1)Out[16]` 0
  • len(dict)计算字典元素个数,即键的总数。
In [26]: dictOut[26]: {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}In [27]: len(dict)Out[27]: 3
  • str(dict)以字符串的形式输出字典
In [28]: str(dict)Out[28]: "{'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}"
  • dict.clear()
    删除字典内所有元素
In [31]: dict.clear()In [32]: dictOut[32]: {}
  • dict.copy()
    返回一个字典的浅复制
In [41]: dictOut[41]: {'Age': 7, 'Class': 'First', 'Name': 'Zara'}In [42]: dict2=dict.copy()In [43]: dict2Out[43]: {'Age': 7, 'Class': 'First', 'Name': 'Zara'}
  • dict.fromkeys(seq[, val]))
    创建一个新字典,以序列 seq 中元素做字典的键,val 为字典所有键对应的初始值
seq = ('name', 'age', 'sex')dict = dict.fromkeys(seq)print "New Dictionary : %s" %  str(dict)dict = dict.fromkeys(seq, 10)print "New Dictionary : %s" %  str(dict)

输出结果:

New Dictionary : {'age': None, 'name': None, 'sex': None}New Dictionary : {'age': 10, 'name': 10, 'sex': 10}
  • dict.get(key, default=None)
    返回指定键的值,如果值不在字典中返回default值
dict = {'Name': 'Zara', 'Age': 27}print "Value : %s" %  dict.get('Age')print "Value : %s" %  dict.get('Sex', "Never")Value : 27Value : Never
  • dict.has_key(key)
    如果键在字典dict里返回true,否则返回false
dict = {'Name': 'Zara', 'Age': 7}print "Value : %s" %  dict.has_key('Age')print "Value : %s" %  dict.has_key('Sex')Value : TrueValue : False
  • dict.items()函数以列表返回可遍历的(键, 值) 元组数组
In [51]: dict2 = {'Name': 'Zara', 'Age': 7}In [52]: dict2.items()Out[52]: [('Age', 7), ('Name', 'Zara')]In [53]: l=dict2.items()In [54]: l[1]Out[54]: ('Name', 'Zara')In [55]: l[0]Out[55]: ('Age', 7)
  • dict.setdefault(key, default=None)
    和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default

  • dict.update(dict2)把字典dict2的键/值对追加到dict里

In [56]: dict = {'Name': 'Zara', 'Age': 7}    ...: dict2 = {'Sex': 'female' }    ...:    ...: dict.update(dict2)    ...:In [57]: dictOut[57]: {'Age': 7, 'Name': 'Zara', 'Sex': 'female'}
  • dict.keys(),dict.values()返回字典的键/值对组成的列表
In [57]: dictOut[57]: {'Age': 7, 'Name': 'Zara', 'Sex': 'female'}In [58]: dict.keys()Out[58]: ['Age', 'Name', 'Sex']In [59]: dict.values()Out[59]: [7, 'Zara', 'female']
0 0