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

作者:hebedich 时间:2022-06-14 16:31:31 

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


items=[('name','zhang'),('age',42)]
d=dict(items)
d['name']

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


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

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


x.clear()

copy:浅复制字典。


y=x.copy()

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


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


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


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


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


d.get('name')



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

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

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

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

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

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

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

popitem:弹出一个随机的项,

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

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


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

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

itervalues:返回值得迭代器。

标签:python,字典
0
投稿

猜你喜欢

  • Pyqt5 实现窗口缩放,控件在窗口内自动伸缩的操作

    2022-10-16 06:32:09
  • MySQL数据库中修改密码及访问限制设置详解

    2009-09-01 14:19:00
  • python利用scatter绘画散点图

    2021-02-02 01:45:37
  • golang gorm实现get请求查询案例测试

    2024-05-09 10:09:31
  • CI框架整合smarty步骤详解

    2023-11-14 11:18:11
  • Python字符编码转码之GBK,UTF8互转

    2023-02-20 14:03:01
  • 深入浅析mybatis oracle BLOB类型字段保存与读取

    2024-01-15 02:21:26
  • mysql中的int(10)int(20)分别代表什么意思

    2024-01-20 21:31:30
  • sql2005 数据同步方法

    2024-01-15 03:28:54
  • 有效地使用 SQL事件探查器的提示和技巧

    2009-01-15 13:39:00
  • Python绘画好看的星空图

    2021-11-22 12:48:12
  • 在生成的静态页面中统计点击次数

    2009-11-19 13:20:00
  • Python使用itchat 功能分析微信好友性别和位置

    2023-09-24 15:57:12
  • Python集合set()使用的方法详解

    2022-11-16 21:43:03
  • Python Pandas高级教程之时间处理

    2021-08-12 14:04:49
  • 详解Python如何获取列表(List)的中位数

    2022-02-01 02:35:37
  • js版实现计算器功能

    2024-04-23 09:26:42
  • 理解Sql Server中的聚集索引

    2024-01-23 11:51:46
  • MySQL中使用case when 语句实现多条件查询的方法

    2024-01-16 17:17:31
  • 用python登录带弱图片验证码的网站

    2023-04-28 12:22:22
  • asp之家 网络编程 m.aspxhome.com