字典特点:无序、键唯一

字典的创建

bag = {'cx':'chenxi','gghg':35}
print(bag['cx'])

  测试

chenxi

Process finished with exit code 0

  字典操作之增加

cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['zd']='zrd'
print(cx)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'chenxi', 'user': 'haha', 'zd': 'zrd'}

  字典操作之修改值操作

cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}

  字典操作之新增操作

cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
ret=cx.setdefault('cx',89) #如果键有值,就不会修改,返回对应键的值
print(ret)
cxs=cx.setdefault('df',78) #如果没有,直接创建并并赋值
print(cxs)
print(cx)

  测试

{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
dne
78
{'cx': 'dne', 'user': 'haha', 'df': 78}

  查看字典里所有的键

cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
ret=cx.setdefault('cx',89) #如果键有值,就不会修改,返回对应键的值
print(ret)
cxs=cx.setdefault('df',78) #如果没有,直接创建并并赋值
print(cxs)
print(cx)
print(cx.keys()) #查看字典里所有的键

  测试

{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
dne
78
{'cx': 'dne', 'user': 'haha', 'df': 78}
dict_keys(['cx', 'user', 'df'])

  查看字典中所有的键;并转换成列表数据类型

cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
ret=cx.setdefault('cx',89) #如果键有值,就不会修改,返回对应键的值
print(ret)
cxs=cx.setdefault('df',78) #如果没有,直接创建并并赋值
print(cxs)
print(cx)
print(list(cx.keys())) #查看字典里所有的键;并把它转换成列表数据结构

  测试

{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
dne
78
{'cx': 'dne', 'user': 'haha', 'df': 78}
['cx', 'user', 'df']

  查看字典中所有的值,并以列表方式显示

cx = {'cx':'chenxi','user':'haha'}
print(cx)
cx['cx']='dne'
print(cx)
ret=cx.setdefault('cx',89) #如果键有值,就不会修改,返回对应键的值
print(ret)
cxs=cx.setdefault('df',78) #如果没有,直接创建并并赋值
print(cxs)
print(cx)
print(list(cx.keys())) #查看字典里所有的键;并把它转换成列表数据结构
print(list(cx.values())) #查看字典所有值,并转换成列表数据结构

  测试

{'cx': 'chenxi', 'user': 'haha'}
{'cx': 'dne', 'user': 'haha'}
dne
78
{'cx': 'dne', 'user': 'haha', 'df': 78}
['cx', 'user', 'df']
['dne', 'haha', 78]

  修改字典里键的值

dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
print(dis3)
dis3['age']=55 #age的值改55
print(dis3)

  测试

{'age': 18, 'name': 'chenxi', 'hobby': '阅读'}
{'age': 55, 'name': 'chenxi', 'hobby': '阅读'} Process finished with exit code 0

  更新

dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
print(dis3)
cx7={'sdf':'csd','ga':'gffg','yu':'ggh'}
print(cx7)
dis3.update(cx7)
print(dis3)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{'age': 18, 'name': 'chenxi', 'hobby': '阅读'}
{'sdf': 'csd', 'ga': 'gffg', 'yu': 'ggh'}
{'age': 18, 'name': 'chenxi', 'hobby': '阅读', 'sdf': 'csd', 'ga': 'gffg', 'yu': 'ggh'} Process finished with exit code 0

  删

dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
del dis3['age']
print(dis3)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{'name': 'chenxi', 'hobby': '阅读'} Process finished with exit code 0

  清空字典操作

dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
dis3.clear()
print(dis3)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{} Process finished with exit code 0

  删除字典中某键值并把所删的值重新打印出来

dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
ret = dis3.pop('age')
print(dis3)
print(ret)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{'name': 'chenxi', 'hobby': '阅读'}
18 Process finished with exit code 0

  随机删除一对键值,并把删除的这对键值打印出来

dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
ret = dis3.popitem()
print(dis3)
print(ret)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{'age': 18, 'name': 'chenxi'}
('hobby', '阅读')

  删除这个字典

dis3 = {'age':18,'name':'chenxi','hobby':'阅读'}
del dis3
print(dis3)

  测试

D:\python\python.exe D:/untitled/dir/for.py
Traceback (most recent call last):
File "D:/untitled/dir/for.py", line 126, in <module>
print(dis3)
NameError: name 'dis3' is not defined

  创建值相同的字典

dic6 = dict.fromkeys(['cx-1','cx-2','cx-3'],'test')
print(dic6)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{'cx-1': 'test', 'cx-2': 'test', 'cx-3': 'test'} Process finished with exit code 0

  注意

dic6 = dict.fromkeys(['cx-1','cx-2','cx-3'],['test-1','test-2'])
print(dic6)
dic6['cx-2'][1]='abc'
print(dic6)

  测试

{'cx-1': ['test-1', 'test-2'], 'cx-2': ['test-1', 'test-2'], 'cx-3': ['test-1', 'test-2']}
{'cx-1': ['test-1', 'abc'], 'cx-2': ['test-1', 'abc'], 'cx-3': ['test-1', 'abc']}

  嵌套字典修改

av_cte = {
"中国":{
"河北":["不错","历史"],
"广州":["喜欢","沿海"],
"长沙":["适合玩"],
"北京":["房价死贵"]
},
"消费" :{
"河北":["一般"],
"广州":["还好"],
"上海":["没去过"],
"长沙":["没去过"],
"北京":["小贵"]
}
}
print(av_cte)
av_cte['中国']['广州'][1]="hhh"
print(av_cte)

  测试

D:\python\python.exe D:/untitled/dir/for.py
{'中国': {'河北': ['不错', '历史'], '广州': ['喜欢', '沿海'], '长沙': ['适合玩'], '北京': ['房价死贵']}, '消费': {'河北': ['一般'], '广州': ['还好'], '上海': ['没去过'], '长沙': ['没去过'], '北京': ['小贵']}}
{'中国': {'河北': ['不错', '历史'], '广州': ['喜欢', 'hhh'], '长沙': ['适合玩'], '北京': ['房价死贵']}, '消费': {'河北': ['一般'], '广州': ['还好'], '上海': ['没去过'], '长沙': ['没去过'], '北京': ['小贵']}}

  字典排序

dic = {5:'888',8:'44544',3:'895'}
print(dic)#未排序的
print(sorted(dic))# 键排序
print(sorted(dic.values())) #按键值排序
print(sorted(dic.items())) # 按键排序

  测试

D:\python\python.exe D:/untitled/dir/for.py
{5: '888', 8: '44544', 3: '895'}
[3, 5, 8]
['44544', '888', '895']
[(3, '895'), (5, '888'), (8, '44544')]
Process finished with exit code 0

  字典遍历;效率高

dic = {5:'888',8:'44544',3:'895'}
for i in dic:
print(i,dic[i])

  测试

5 888
8 44544
3 895

  字典遍历之2

dic = {5:'888',8:'44544',3:'895'}
for i,v in dic.items():
print(i ,v)

 测试

5 888
8 44544
3 895

  

  

  

python 基础之字典一的更多相关文章

  1. python基础之字典dict和集合set

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7043642.html python基础之字典dict和集合set 字典dic ...

  2. Python基础数据类型-字典(dict)

    Python基础数据类型-字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版本的哟 ...

  3. python基础之字典、集合

    一.字典(dictionary) 作用:存多个值,key-value存取,取值速度快 定义:key必须是不可变类型,value可以是任意类型 字典是一个无序的,可以修改的,元素呈键值对的形式,以逗号分 ...

  4. Python基础知识---字典

    现在在实习期间,好久没用Python了,今天在做Java项目时用的HashMap让我联想到了Python中的字典,就写一些Python字典的知识吧,复习复习. 字典:  key --> valu ...

  5. python基础5 字典

    一.字典 字典是python的基础数据类型之一:字典可以存储大量的数据,关系型数据. 同样他也是python中唯一的映射类的数据类型. 数据类型的分类: 可变的(不可哈希)数据类型:list,dict ...

  6. python基础_字典_列表_元组考试_day4

    1.请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li=['alex','eric','rain'] li=['alex','eric','rain'] v="_".jo ...

  7. Python基础__字典、集合、运算符

    之前讨论的字符串.列表.元组都是有序对象,本节则重点讨论无序对象:字典与集合.一.字典 列表是Python中的有序集合,列表中的序指的是列表中的元素与自然数集形成了一个一一对应的关系.例如L=['I' ...

  8. python基础类型—字典

    字典 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必 ...

  9. Python 全栈开发二 python基础 字符串 字典 集合

    一.字符串 1,在python中,字符串是最为常见的数据类型,一般情况下用引号来创建字符串. >>ch = "wallace" >>ch1 = 'walla ...

  10. python基础之字典以及增删改查

    字典:字典是python中唯一的一个映射类型,主要形式为 dic = {key1:value,key2:value2,....} 字典中key的值是唯一的,主要关系到HASH算法,并且key的值必须是 ...

随机推荐

  1. SpringMVC的三种处理器适配器

    SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping.SimpleControllerHandlerAdapter.ControllerClassNam ...

  2. Reinforcement Learning Algorithm 资源

    算法源码: https://github.com/ljpzzz/machinelearning https://github.com/imraviagrawal/Reinforcement-Learn ...

  3. 前端之HTML基础篇

    HTML基础篇 目录                                                                               本章内容: 简介 1. ...

  4. SpringCloud-粪发涂墙90

    https://mp.weixin.qq.com/s/UNm8cBw4TKq4OobVKHUBXA 邻国相望,鸡犬之声相闻,民至老死不相往来.这个世界被小诸侯给切的七零八落,一锅乱麻. 而现实是,我的 ...

  5. Mysql2docx自动生成数据库说明文档

    [需要python3.0以上] 首先安装Mysql2docx,如下: pip install Mysql2docx 然后打开pycharm,新建test.py # python from Mysql2 ...

  6. 流式计算(三)-Flink Stream 篇一

    原创文章,谢绝任何形式转载,否则追究法律责任! ​流的世界,有点乱,群雄逐鹿,流实在太多,看完这个马上又冒出一个,也不知哪个才是真正的牛,据说Flink是位重量级选手,能流计算,还能批处理, 和其他伙 ...

  7. django 模版查找路径

    路径的配置,模版上下文的配置等.模版路径可以在两个地方配置. 1.'DIRS':这是一个列表,在这个列表中可以存放所有的模版路径,以后在视图中使用render或者render_to_string渲染模 ...

  8. 查看Oracle的表中有哪些索引(用user_indexes和user_ind_columns)

    用user_indexes和user_ind_columns系统表查看已经存在的索引 对于系统中已经存在的索引我们可以通过以下的两个系统视图(user_indexes和user_ind_columns ...

  9. 使用keil5(MDK)软件的一些记录

    1.如何快速找到相关函数 2.写程序时,换行时,如何使光标自动与上一行的代码自动对齐

  10. 重新梳理IT知识之java-05面向对象(一)

    一.Java面向对象学习的三条主线: 1.Java类及类的成员:属性.方法.构造器:代码块.内部类 2.面向对象的三大特征:封装性.继承性.多态性.(抽象性) 3.其他关键字:this.super.s ...