字典的基本方法

什么是字典:

字典是一种 key - value的数据类型,听alex说就像我们上学用的字典,通过笔划,字母来查找对饮页面的详细内容。

语法:

id_dict = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}

字典的特性:

  dict是无序的

  key必须是唯一的,value可以重复,    key=键,value=值

增加:

id_dict["stu1104"] = "smelond"
print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1104': 'smelond'}

修改:

id_dict["stu1101"] = "amanda"
print(id_dict)
{'stu1101': 'amanda', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

删除里面的某一项:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} id_dict.pop("stu1101")#标准删除
print(id_dict)
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

del删除:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} del id_dict["stu1101"]#del删除
print(id_dict)
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

随机删除:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} id_dict.popitem()#随机删除
print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola'}

直接删字典:

print(id_dict)
id_dict.clear() #删除字典里面的所有内容
print(id_dict) {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
{}

查找:

print("stu1101" in id_dict)#in查看有没有这个对象
True #如果有返回真,没有则fFalse

获取:

print(id_dict.get("stu1101"))#用get获取如果存在返回key值,不存在则返回None,
TengLan Wu
print(id_dict["stu1101"])#这个方法不会像上面那样智能,key不存在就直接报错
TengLan Wu
print(id_dict["stu11231"])
print(id_dict["stu11231"])
KeyError: 'stu11231'

多字典嵌套:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# File_type:多字典嵌套
# Filename:dict_nest.py
# Author:smelond
id_dict = {
"ChengDu": {
"acreage": 14312, "population": "1591.8w", "postalcode": 610000},
"ShenZhen": {
"acreage": 1196, "population": "1190.08w", "postalcode": 518000},
"BeiJing": {
"acreage": "1.641w", "population": "2172.9w", "postalcode": 100000}
} print(id_dict["ChengDu"])#打印输出所有的值
print(id_dict["ChengDu"]["acreage"]) #打印输出成都的面积
id_dict["ChengDu"]["acreage"] = "面 积:14312平方千米" #给成都的面积重新修改为了"面 积:14312平方千米"
print(id_dict["ChengDu"]["acreage"])#打印 {'acreage': 14312, 'population': '1591.8w', 'postalcode': 610000}
14312
面 积:14312平方千米

将字典转换为元组:

print(id_dict.items())
dict_items([('stu1101', 'TengLan Wu'), ('stu1102', 'LongZe Luola'), ('stu1103', 'XiaoZe Maliya')])

将字典转换为列表:

list_test = list(id_dict)
print(list_test) ['stu1101', 'stu1102', 'stu1103']

字典的循环:

for key in id_dict:
print(key, id_dict[key])#由于加入了key,所以他把stu也循环出来了
输出:
stu1101 TengLan Wu
stu1102 LongZe Luola
stu1103 XiaoZe Maliya for key in id_dict:
print(id_dict[key])
输出:
TengLan Wu
LongZe Luola
XiaoZe Maliya

多字典嵌套的循环:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# File_type:多字典嵌套
# Filename:dict_nest.py
# Author:smelond
id_dict = {
"ChengDu": {
"acreage": 14312, "population": "1591.8w", "postalcode": 610000},
"ShenZhen": {
"acreage": 1196, "population": "1190.08w", "postalcode": 518000},
"BeiJing": {
"acreage": "1.641w", "population": "2172.9w", "postalcode": 100000}
} for key in id_dict:#还是照常循环
test = id_dict[key]#将我们每次的key赋给test
for key in test:#再来循环test
# print(key, ":", test[key], "\n", end="")
print("%s : %s" % (key, test[key]))#将第二级的key打印,并且打印出value acreage : 14312
population : 1591.8w
postalcode : 610000
acreage : 1196
population : 1190.08w
postalcode : 518000
acreage : 1.641w
population : 2172.9w
postalcode : 100000

python字典的基本操作的更多相关文章

  1. Python 字典(Dictionary) 基本操作

    Python字典是一种可变容器模型,可存储任意类型对象:如字符串.数字.元组等.它以键值对(key-value)的形式存在,因此相当于Hashmap在python中的实现. §1. 创建字典  字典由 ...

  2. Python: 字典的基本操作

    字典是Python里唯一的映射类型.字典是可变的.无序的.大小可变的键值映射,有时候也称为散列表或关联数组. 例子在下面: dic = {"apple":2, "oran ...

  3. python字典的基本操作,以及可变数据类型和不可变数据类型的区分

    字典:采用键值对存储数据的数据类型,字典的键必须是不可变的数据类型 补充: 不可变(可哈希)数据类型:str,bool,int,tuple 可变(不可哈希)数据类型:list,  dict, set ...

  4. Python 字典方法

    访问字典的值 字典中的 键/值 实际上就是一种映射关系,只要知道了 “键”,就肯定知道 “值”. >>> my_dict = dict(name = 'zhangsan',other ...

  5. Python字典和集合

    Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...

  6. python 字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  7. python字典中的元素类型

    python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...

  8. python字典copy()方法

    python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...

  9. python 字典实现类似c的switch case

    #python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...

随机推荐

  1. redis学习(三)redis持久化

    redis持久化 1.redis持久化介绍 我们知道redis性能之所以强悍,是因为redis在运行时将数据都存放在了访问效率远高于硬盘的内存之中.可是这带来了新的问题:在redis或者外部系统重启时 ...

  2. python中os模块的walk函数

    Python的文档中对walk的介绍: walk(top, topdown=True, onerror=None, followlinks=False) 树状目录的生成器. 对于以top参数为根的目录 ...

  3. 第一次项目上Linux服务器(二:——安装jdk)

    本人采用的是rpm安装jdk1.8 1.下载jdk 去jdk下载页面找到要下载的jdk 本人下载的是jdk-8u161-linux-x64.rpm,百度云资源链接:链接:https://pan.bai ...

  4. FFmpeg简易播放器的实现-音视频同步

    本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10284653.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...

  5. JavaScript的3种继承方式

    JavaScript的继承方式有多种,这里列举3种,分别是原型继承.类继承以及混合继承. 1.原型继承 优点:既继承了父类的模板,又继承了父类的原型对象: 缺点:不是子类实例传参,而是需要通过父类实例 ...

  6. POJ 1061 青蛙的约会(拓展欧几里得算法求解模线性方程组详解)

    题目链接: BZOJ: https://www.lydsy.com/JudgeOnline/problem.php?id=1477 POJ: https://cn.vjudge.net/problem ...

  7. .NET平台开源文档与报表处理组件包括Execel PDF Word等

    在前2篇文章这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧 和这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,大伙热情高涨.再次拿出自己的私货,在.NET平台 ...

  8. iis 程序池设置及详解-20180720

    [非原创,好文收藏,分享大家] 公司的一个网站程序长时间运行后,速度变慢,重新启动网站后速度明显变快,估计是网站程序占用的内存和CPU资源没能及时释放,才需要每隔一段时间重启网站释放资源.但手工重启总 ...

  9. [android] 切换按钮-自定义控件-拖动效果

    重写View的onTouchEvent()方法,传递进来MotionEvent对象 调用MotionEvent对象的getAction()方法,获取当前动作 switch判断一下当前动作 事件为Mot ...

  10. springboot中使用mybatis之mapper

    Spring Boot中使用MyBatis传参方式:使用@Param@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})& ...