博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 数据类型-3
阅读量:6497 次
发布时间:2019-06-24

本文共 3708 字,大约阅读时间需要 12 分钟。

字典 (dict)

Python中唯一的映射的类型(哈希表)字典对象是可变的,但是字典的键必须使用不可变的对象,一个字典可以使用不同类型的键值

定义字典:

In [68]: dic = {}   #使用 {}创建空字典In [69]: dicOut[69]: {}In [70]: type(dic)Out[70]: dictIn [71]: dic = {'a':1, 1:'abc'}   #使用 {} 创建字典In [72]: dicOut[72]: {1: 'abc', 'a': 1}   #无序In [73]: type(dic)Out[73]: dictIn [77]: list1 = ['name', 'age'] #将列表合并为字典In [78]: list2 = ['tom', 20]In [79]: dict(zip(list1, list2))  #dict()函数创建字典Out[79]: {'age': 20, 'name': 'tom'}In [81]: dict(x=10,y=11)   ##使用dict()函数创建字典Out[81]: {'x': 10, 'y': 11} In [82]: dict.fromkeys('abce',100) ##使用dict.fromkeys()方法创建字典Out[82]: {'a': 100, 'b': 100, 'c': 100, 'e': 100}

方法:

get() 根据键获取值

k = {"name":"lijunjiang", "age":20}print(k.get("name"))print(k.get("age"))print(k.get("address"))执行:C:\Python27\python.exe D:/Python/type-of-data1.pylijunjiang20None  #没有的键值返回 NoneProcess finished with exit code 0

setdefault() 设置默认值,当KEY有值时,保持原值,当key没有值则设置该key的值

k = {"name":"lijunjiang", "age":20}print(k.setdefault("name", "libai"))print(k.setdefault("age", 19))print(k.setdefault("address", "beijing"))print(k)执行:C:\Python27\python.exe D:/Python/type-of-data1.pylijunjiang20beijing{'age': 20, 'name': 'lijunjiang', 'address': 'beijing'}Process finished with exit code 0

keys() 获的字典的Key (一次性取出所有)

iterkeys() 获得一个对象 (逐个取出) 效率高

k = {"name":"lijunjiang", "age":20}print(k.keys())执行:C:\Python27\python.exe D:/Python/type-of-data1.py['age', 'name']Process finished with exit code 0

values() 获取所有key的值(一次性取出所有)

iteritems() 获得一个对象(逐个取出) 效率高

k = {"name":"lijunjiang", "age":20}print(k.values())执行:C:\Python27\python.exe D:/Python/type-of-data1.py[20, 'lijunjiang']Process finished with exit code 0

iterkey iteritems items(逐个将key:values取出返回一个列表)

k = {"name":"lijunjiang", "age":20}for x, y in k.iteritems():    print( x , y)print(k.items())执行:C:\Python27\python.exe D:/Python/type-of-data1.py('age', 20)('name', 'lijunjiang')[('age', 20), ('name', 'lijunjiang')]Process finished with exit code 0

pop() 删除某个在的键值,必须输入一个存在的key,否则会报错

k = {"name":"lijunjiang", "age":20}k.setdefault("addr", "beijing")print(k)k.pop("name")print(k)k.pop("addr")print(k)执行:C:\Python27\python.exe D:/Python/type-of-data1.py{'age': 20, 'name': 'lijunjiang', 'addr': 'beijing'}{'age': 20, 'addr': 'beijing'}{'age': 20}Process finished with exit code 0

fromkeys()

list1 = ["a", "b", "c", "d"]k = {}n = k.fromkeys(list1, 123)print(n)执行:C:\Python27\python.exe D:/Python/type-of-data1.py{'a': 123, 'c': 123, 'b': 123, 'd': 123}Process finished with exit code 0

zip()

list1 = ["a", "b", "c", "d"]list2 = [12, 234, 32, 23]n = zip(list1, list2)print(n)执行:C:\Python27\python.exe D:/Python/type-of-data1.py[('a', 12), ('b', 234), ('c', 32), ('d', 23)]Process finished with exit code 0#########################################################33dict() 列表转换成字典list1 = ["a", "b", "c", "d"]list2 = [12, 234, 32, 23]n = dict(zip(list1, list2))print(n)执行:C:\Python27\python.exe D:/Python/type-of-data1.py{'a': 12, 'c': 32, 'b': 234, 'd': 23}Process finished with exit code 0

dict.updata()

list1 = ["a", "b", "c", "d"]list2 = [12, 234, 32, 23]n = dict(zip(list1, list2))print(n)k = {"name": "lijunjiang", "age": 20}k.update(n)print(k)执行:C:\Python27\python.exe D:/Python/type-of-data1.py{'a': 12, 'c': 32, 'b': 234, 'd': 23}{'a': 12, 'c': 32, 'b': 234, 'name': 'lijunjiang', 'age': 20, 'd': 23}Process finished with exit code 0

sorted()

mm = dict(a=1, b=120, c=5, d=50)print sorted(mm.iteritems(), key = lambda d: d[1], reverse=True)print sorted(mm.iteritems(), key = lambda a: a[0], reverse=True)执行:C:\Python27\python.exe D:/Python/type-of-data1.py[('b', 120), ('d', 50), ('c', 5), ('a', 1)][('d', 50), ('c', 5), ('b', 120), ('a', 1)]Process finished with exit code 0

转载于:https://www.cnblogs.com/lijunjiang2015/p/7720248.html

你可能感兴趣的文章
JQuery页面随滚动条动态加载效果实现
查看>>
使用PostgreSQL 9.6 架设mediawiki服务器
查看>>
数据库服务器硬件对性能的影响
查看>>
LVM
查看>>
windows+群辉服务器环境下,搭建git版本管理
查看>>
Boolean类型
查看>>
Ubuntu 修改源
查看>>
php 几个比较实用的函数
查看>>
(译)OpenGL ES2.0 – Iphone开发指引
查看>>
@RestController 与 @RequestMapping
查看>>
黑马程序员.bobo.DAY.1
查看>>
Unity shader 官网文档全方位学习(二)
查看>>
pbrun
查看>>
Java后端工程师学习大纲
查看>>
浏览器加载和渲染网页顺序
查看>>
微服务架构springcloud
查看>>
深入剖析Android系统试读样章
查看>>
测试用例出错重跑--flaky插件
查看>>
yaf的安装
查看>>
比较java与C++的不同
查看>>