5、Python-字典
定义
info = {'name': '班长', 'id': 88, 'sex': 'man', 'address': '地球亚洲中国北京'}
print(info['name'])
print(info['address'])
# 若访问不存在的键,则会报错
# print(info['age'])
# 不确定字典中是否存在某个键而又想获取其值时,使用get方法,还可以设置默认值
print(info.get('age',22))
字典的操作
修改
info = {'name': '班长', 'id': 100, 'sex': 'f', 'address': '地球亚洲中国北京'}
info['id'] = int(150301016)
print('修改之后的id为:%d' % info['id'])
添加
info = {'name': '班长', 'sex': 'f', 'address': '地球亚洲中国北京'}
info['id'] = int(150301016)
print('添加之后的id为:%d' % info['id'])
删除
# 删除指定的元素
info = {'name': '班长', 'sex': 'f', 'address': '地球亚洲中国北京'}
del info['name']
info.get("name") # 删除整个字典
info = {'name': 'monitor', 'sex': 'f', 'address': 'China'}
del info
print(info)
# NameError: name 'info' is not defined # 清空整个字典
info = {'name': 'monitor', 'sex': 'f', 'address': 'China'}
info.clear()
print(info)
# {}
相关方法
info = {'name': 'monitor', 'sex': 'f', 'address': 'China'}
# 键值对的个数
print(len(info))
#
# 返回一个包含字典所有KEY的列表
print(info.keys())
# dict_keys(['name', 'sex', 'address'])
# 返回一个包含字典所有value的列表
print(info.values())
# dict_values(['monitor', 'f', 'China'])
# 返回一个包含所有(键,值)元组的列表
print(info.items())
# dict_items([('name', 'monitor'), ('sex', 'f'), ('address', 'China')])
# 如果key在字典中,返回True,否则返回False
print("x" in info)
# False
遍历
info = {'name': 'monitor', 'sex': 'f', 'address': 'China'}
# 遍历字典的key
for key in info.keys():
print(key, end=" ")
# name sex address
# 遍历字典的value
for val in info.values():
print(val, end=" ")
# monitor f China
# 遍历字典的元素
for item in info.items():
print(item, end=" ")
# ('name', 'monitor') ('sex', 'f') ('address', 'China')
# 遍历字典的key-value
for key, val in info.items():
print("k=%s,v=%s" % (key, val), end=" ")
# k=name,v=monitor k=sex,v=f k=address,v=China
其他遍历
# 带下标索引的遍历
chars = ['a', 'b', 'c', 'd']
i = 0
for chr in chars:
print("%d %s" % (i, chr))
i += 1
for i, chr in enumerate(chars):
print(i, chr)
# 字符串遍历
a_str = "hello swt"
for char in a_str:
print(char, end=' ')
# h e l l o s w t # 列表遍历
a_list = [1, 2, 3, 4, 5]
for num in a_list:
print(num, end=' ')
# 1 2 3 4 5 # 元组遍历
a_turple = (1, 2, 3, 4, 5)
for num in a_turple:
print(num, end=" ")
# 1 2 3 4 5
5、Python-字典的更多相关文章
- Python字典和集合
Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- python字典中的元素类型
python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...
- python字典copy()方法
python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...
- python 字典实现类似c的switch case
#python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...
- python字典的常用操作方法
Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...
- Python 字典(Dictionary)操作详解
Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python 字典(Dictionary) setdefault()方法
描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...
- python 字典内置方法get应用
python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...
随机推荐
- html之间传递参数
转自:http://blog.163.com/yangzhanghui_job/blog/static/179575062201271624839972/ aa.html 往 bb.html 传参 a ...
- 小学四则运算APP 第三阶段冲刺-第一天
团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第三次冲刺阶段时间:12.12~12.19 本次发布的是音乐播放功能,可以根据用户需求一边播放音乐一边做题,也拥有暂停播放音乐的功能,增强 ...
- (Alpha)Let's-M1后分析报告
Chronos团队Let's项目 Postmortem结果 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 在最初的用户需求和市场调研方面,团队进 ...
- Machine Learning Based Proactive Flow Entry Deletion for OpenFlow
来源:IEEE 2018 作者:Hemin Yang and George F.Riley 摘要: 流表容量有限,因此高效管理流表至关重要.本文重点讨论了OpenFlow中定义的一种流表管理机制,即能 ...
- shell脚本--制作自己的服务脚本
首先注意一下,我用的环境是centos6.5,中间有一些操作和在Ubuntu上有一些地方的操作是不同的, 编写脚本 首先看一个实例:假设有一个test的服务,可以通过命令对test进行启动.关闭或者重 ...
- git 查看远程分支最后一次提交时间
背景 因为工程创建时间很长了,项目又特别多,导致代码库中远程分支有100多.想要清理一下远程分支,但又不能盲目的删除,需要一定的参考信息. 可以通过代码最后提交时间来进行判断,但是100多个分支,一个 ...
- add (db.collection.add)添加数据
db.collection('cheshi').add({ data: { cheshi:4, } }).then((res) => { console.log(res) })
- python之匿名函数lambda
# lambda:匿名函数 # 语法:lambda 参数: 表达式 answer = lambda x: x * 5 print("answer(5): ", answer(5)) ...
- Lodop多分出空白页的可能(情况1)
在用Lodop进行打印超文本的时候,本身内容看上去只有一页,却分页分出空白的一页,很有可能有不可见内容的存在,下面是测试的一种情况,如html内部有内容占着空间,却是不可见的,如一些对象,或者如测试内 ...
- solr string类型表示不支持分词
solr string类型表示不支持分词