• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python学习笔记:字典的使用示例详解

python 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

经典字典使用函数
dict:通过其他映射(比如其他字典)或者(键,值)这样的序列对建立字典。当然dict成为函数不是十分确切,它本质是一种类型。如同list。

items=[('name','zhang'),('age',42)]<BR><span>本文来源gaodai#ma#com搞*!代#%^码网5</span>d=dict(items)<BR>d['name']<BR>

len(d):返回项的数量
d[k]:返回键k上面的值。
d[k]=v:将k对应的值设置为k。
del d[k]:删除字典中的这一项。
k in d:检查d中是否含有键为k的项。注:只能查找键,不能查找值。
简单的电话本示例:

# A simple database<BR># A dictionary with person names as keys. Each person is represented as<BR># another dictionary with the keys 'phone' and 'addr' referring to their phone<BR># number and address, respectively.<BR>people = {<BR>    'Alice': {<BR>        'phone': '2341',<BR>        'addr': 'Foo drive 23'<BR>    },<BR>    'Beth': {<BR>        'phone': '9102',<BR>        'addr': 'Bar street 42'<BR>    },<BR>    'Cecil': {<BR>        'phone': '3158',<BR>        'addr': 'Baz avenue 90'<BR>    }<BR>}<BR># Descriptive labels for the phone number and address. These will be used<BR># when printing the output.<BR>labels = {<BR>    'phone': 'phone number',<BR>    'addr': 'address'<BR>}<BR>name = raw_input('Name: ')<BR># Are we looking for a phone number or an address?<BR>request = raw_input('Phone number (p) or address (a)? ')<BR># Use the correct key:<BR>if request == 'p': key = 'phone'<BR>if request == 'a': key = 'addr'<BR># Only try to print information if the name is a valid key in<BR># our dictionary:<BR>if name in people: print "%s's %s is %s." % \<BR>    (name, labels[key], people[name][key])<BR>

字典方法
clear:清除字典中的所有项。

x.clear()<BR>

copy:浅复制字典。

y=x.copy()<BR>

deepcopy:同样是复制,来看看和copy的区别。

from copy import deepcopy<BR>d={}<BR>d['names']=['as','sa']<BR>c=d.copy()<BR>dc=deepcopy(d)<BR>d['names'].append('ad')<BR>


fromkeys:给指定的键建立新的字典,每个键默认对应的值为none.

{}.fromkeys(['name','age'])<BR>


get:更为宽松的访问字典项的方法。

d.get('name')<BR>


# A simple database using get()<BR># Insert database (people) from Listing 4-1 here.<BR>labels = {<BR>    'phone': 'phone number',<BR>    'addr': 'address'<BR>}<BR>name = raw_input('Name: ')<BR># Are we looking for a phone number or an address?<BR>request = raw_input('Phone number (p) or address (a)? ')<BR># Use the correct key:<BR>key = request # In case the request is neither 'p' nor 'a'<BR>if request == 'p': key = 'phone'<BR>if request == 'a': key = 'addr'<BR># Use get to provide default values:<BR>person = people.get(name, {})<BR>label = labels.get(key, key)<BR>result = person.get(key, 'not available')<BR>print "%s's %s is %s." % (name, label, result)<BR>

has_key:检查字典中是否含有给定的键。d.haos_key()。值返回True ,False。

items:将所有字典项目一列表方式返回。

iteritems:方法大致相同,但是会返回一个迭代器而不是列表。

keys:将字典中的键以列表的方式返回。(注意区分和items的区别)

iterkeys:返回针对键的迭代器。

pop:获得对应给定键的值,然后将键-值对删除。

popitem:弹出一个随机的项,

setdefault:既能获得与给定键相关的值,又能在字典中不含有该键的情况下设定相应的键值。

update:用一个字典更新另一个字典。

d={'1':'d','2':'s','3':'a'}<BR>x={'1','jk'}<BR>d.update(x)<BR>

values:以列表的形式返回字典中的值。

itervalues:返回值得迭代器。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python学习笔记:字典的使用示例详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址