字典特点:无序、键唯一

字典的创建

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

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的基础字典_python 基础之字典一相关推荐

  1. 视频教程-快速入门Python基础教程_Python基础知识大全-Python

    快速入门Python基础教程_Python基础知识大全 十余年计算机技术领域从业经验,在中国电信.盛大游戏等多家五百强企业任职技术开发指导顾问,国内IT技术发展奠基人之一. 杨千锋 ¥99.00 立即 ...

  2. 视频教程-快速入门Python基础教程_Python基础进阶视频-Python

    快速入门Python基础教程_Python基础进阶视频 十余年计算机技术领域从业经验,在中国电信.盛大游戏等多家五百强企业任职技术开发指导顾问,国内IT技术发展奠基人之一. 杨千锋 ¥199.00 立 ...

  3. python.集合转列表_Python基础数据类型:元组、列表、字典、集合

    你好,我是goldsunC. 让我们一起进步吧! 元组.列表.字典.集合 元组和列表是Python语言中非常重要的一部分内容,它们是Python中除了字符串以外的另两种序列结构,几乎在任何地方都少不了 ...

  4. python 类 字典_python基础类型—字典

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

  5. python常用变量名_python基础知识整理

    Python Python开发 Python语言 python基础知识整理 序言:本文简单介绍python基础知识的一些重要知识点,用于总结复习,每个知识点的具体用法会在后面的博客中一一补充程序: 一 ...

  6. python之禅星号_Python基础1

    介绍Python基础,包括变量和简单数据类型.列表基础.操作列表.if语句.字典.输入函数和while循环 在文件名和文件夹名中,最好使用小写字母,并使用下划线来表示空格,因为这是Python采用的命 ...

  7. python csv写入字典_python csv与字典操作

    # encoding: utf-8 import csv d1 = {'banana':3,'apple':4,'pear':1,'orange':2} d2 = {'banana':3,'orang ...

  8. python文件目录操作操作_Python基础之文件目录操作

    概述 I/O操作不仅包括屏幕输入输出,还包括文件的读取与写入,Python提供了很多必要的方法和功能,进行文件及文件夹的相关操作.本文主要通过两个简单的小例子,简述Python在文件夹及文件的应用,仅 ...

  9. python内置数据结构_Python基础知识2-内置数据结构(下)

    bytes.bytearray #思考下面例子: a = 1b=aprint(a == b)#True print(a is b)#True print(id(a) is id(b))#False p ...

  10. python唯一映射类型_Python基础:04映射类型

    字典是Python语言中唯一的映射类型.一个字典对象是可变的,它是一个容器类型,能存储任意个数的Python对象.字典中的数据是无序排列的. 映射类型也可被称做哈希表,哈希表的算法是获取键,对键执行一 ...

最新文章

  1. MYSQL中TIMESTAMP类型的默认值
  2. pandas 季度_pandas_时间序列和常用操作
  3. python---os
  4. 牛顿法与拟牛顿法,SDM方法的一些注记
  5. Lisp入门(好文)
  6. PKD-Bert:基于多层网络的Bert知识蒸馏
  7. java证明角谷猜想_Java 代码界 3% 的王者?看我是如何解错这 5 道题的
  8. 迅雷精简版 Mac中文版
  9. matlab 三角函数积分,正弦函数与三角函数积分及Matlab编程.doc
  10. Flutter-防京东商城项目-修改默认收货地址 显示默认收货地址-42
  11. No MyBatis mapper was found in ‘[xx.mapper]‘ package. Please check your configuration.特殊处理
  12. Js-Html转文本
  13. QtCreator编译错误: -1: error: [debug/main.o] Error 1 问题的解决办法
  14. codeforces-1202C-WASD-string
  15. 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
  16. EDM营销进行消费场景布置的案例分享
  17. 购物网站的html5页面,网购商城(html5页面设计)
  18. java找不到或无法加载主类
  19. 笔记:TRULY_SPINACH(优酷播单:和我一起用Unity3D玩游戏)
  20. 电梯轴承市场现状及未来发展趋势分析

热门文章

  1. mysql 不显示消息错误_如何编写不吸的错误消息
  2. SAS在金融中的应用四
  3. go编译go-gtk报错
  4. shell编程之进阶篇五函数
  5. ASP.NET企业开发框架IsLine FrameWork系列之十五--框架配置信息大全(下)
  6. 什么是Web 2.0——下一代软件的设计模式和商业模式 (全文翻译—1 博客版序)
  7. 使用RDLC报表(四)--钻取式报表
  8. 内容页响应母版页控件的事件
  9. 转帖:免费申请样片的公司大全!!
  10. 吴恩达深度学习 —— 4.5 搭建深层神经网络块