Python<字典>

  • 字典的每个键值(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]: dict
Out[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]: dict
Out[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]: dict
Out[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]: dict
Out[15]: {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

In [16]: cmp(dict,dict1)
Out[16]`
 0
  • len(dict)计算字典元素个数,即键的总数。
In [26]: dict
Out[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]: dict
Out[32]: {}
  • dict.copy()
    返回一个字典的浅复制
In [41]: dict
Out[41]: {'Age': 7, 'Class': 'First', 'Name': 'Zara'}

In [42]: dict2=dict.copy()

In [43]: dict2
Out[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 : 27
Value : 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 : True
Value : 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]: dict
Out[57]: {'Age': 7, 'Name': 'Zara', 'Sex': 'female'}
  • dict.keys(),dict.values()返回字典的键/值对组成的列表
In [57]: dict
Out[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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值