字典特点:无序、键唯一

字典的创建

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. [lua]紫猫lua教程-命令宝典-L1-01-06. 循环结构

    L1[循环]01. for循环结构介绍 只是简单的说了下计数型的for循环结构 for i=1,10,1 do testlib.traceprint(i) end 注意几点: 1.上面的1和10表示循 ...

  2. jsp+servlet实现的验证登陆

    可以将业务逻辑处理和视图相分离,使用jsp界面表示视图,使用servlet处理业务逻辑 login.jsp <%@ page language="java" contentT ...

  3. 【PAT甲级】1111 Online Map (30分)(dijkstra+路径记录)

    题意: 输入两个正整数N和M(N<=500,M<=N^2),分别代表点数和边数.接着输入M行每行包括一条边的两个结点(0~N-1),这条路的长度和通过这条路所需要的时间.接着输入两个整数表 ...

  4. 【PAT甲级】1104 Sum of Number Segments (20 分)

    题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...

  5. Go_runtime包

    package main import ( "fmt" "runtime" "time" ) //写在init函数里,main函数运行之前就 ...

  6. getter和setter

    /* 对象属性是由名字.值和自足特性构成的. 属性值可以用一个或两个方法替代,这两个方法就是getter和setter. 由getter和setter定义的属性称作“存取器属性” */ /* 定义存取 ...

  7. spring 基于XML的申明式AspectJ通知的执行顺序

    spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...

  8. 华硕笔记本(i76700hq+nvidia goforce940mx)安装ubuntu18.04

    Ubuntu的安装 今天终于下定决心要把笔记本安装成Ubuntu,但是网上的教材不够全面,我就想整合以下教程. 接下来详细讲解安装过程 1. Ubuntu iso镜像下载 下载地址:https://w ...

  9. leetcode 387

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  10. placeholder样式

    .mdwh_txtmod_tp_inpshad input::-webkit-input-placeholder { /* WebKit browsers */ color: #cccccc; } . ...