字典特点:无序、键唯一

字典的创建

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. 随机获取list或set或map中的一个元素

    转自:https://m.2cto.com/kf/201507/412937.html import java.util.HashSet;import java.util.List;import ja ...

  2. 【转】Chrome——F12 谷歌开发者工具详解

    Chrome——F12 谷歌开发者工具详解 console source network

  3. mybatis--一对多关联

    今天来介绍mybatis的一对多关联 (1)首先创建数据库mybatisonetomany,并创建数据库表post和user,并向其中插入一定的数据: create database mybatiso ...

  4. LOJ#6713. 「EC Final 2019」狄利克雷 k 次根 加强版

    题目描述 定义两个函数 \(f, g: \{1, 2, \dots, n\} \rightarrow \mathbb Z\) 的狄利克雷卷积 \(f * g\) 为: \[ (f * g)(n) = ...

  5. 对于使用secureFX上传文件到centos7 的时候,以及不同的用户解压文件,对于文件操作权限的实验

    本以为以一个用户胡如root登录了SecureFx,之后选择了root的家目录下的一个software目录,之后上传 以root用户远程登录LINUX系统 查看文件 之后再验证普通用户zhaijh登录 ...

  6. 单选按钮 设置required属性无法进行非空验证

    先看代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. Python学习之字典集合篇

    lambda表达式:起到一个函数速写的作用,允许在代码内嵌入一个函数的定义; filter()函数:1.用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表.2.接收两个参数,第一个为 ...

  8. 改变input[type=range]的样式 动态滑动

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  9. ES6-三点运算符

    首先理解一下函数总的arguments变量,这个变量是函数内部自动生成的,他用来保存传入函数的实参,是一个伪数组. 例: function fun(a,b){ console.log(argument ...

  10. PostgreSQL日期加减

    在PostgreSQL中可以直接对时间进行加减运算:. SELECT now()::timestamp + '1 year';  --当前时间加1年 SELECT now()::timestamp + ...