字典的基本方法

什么是字典:

字典是一种 key - value的数据类型,听alex说就像我们上学用的字典,通过笔划,字母来查找对饮页面的详细内容。

语法:

id_dict = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}

字典的特性:

  dict是无序的

  key必须是唯一的,value可以重复,    key=键,value=值

增加:

id_dict["stu1104"] = "smelond"
print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1104': 'smelond'}

修改:

id_dict["stu1101"] = "amanda"
print(id_dict)
{'stu1101': 'amanda', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

删除里面的某一项:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} id_dict.pop("stu1101")#标准删除
print(id_dict)
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

del删除:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} del id_dict["stu1101"]#del删除
print(id_dict)
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

随机删除:

print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} id_dict.popitem()#随机删除
print(id_dict)
{'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola'}

直接删字典:

print(id_dict)
id_dict.clear() #删除字典里面的所有内容
print(id_dict) {'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
{}

查找:

print("stu1101" in id_dict)#in查看有没有这个对象
True #如果有返回真,没有则fFalse

获取:

print(id_dict.get("stu1101"))#用get获取如果存在返回key值,不存在则返回None,
TengLan Wu
print(id_dict["stu1101"])#这个方法不会像上面那样智能,key不存在就直接报错
TengLan Wu
print(id_dict["stu11231"])
print(id_dict["stu11231"])
KeyError: 'stu11231'

多字典嵌套:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# File_type:多字典嵌套
# Filename:dict_nest.py
# Author:smelond
id_dict = {
"ChengDu": {
"acreage": 14312, "population": "1591.8w", "postalcode": 610000},
"ShenZhen": {
"acreage": 1196, "population": "1190.08w", "postalcode": 518000},
"BeiJing": {
"acreage": "1.641w", "population": "2172.9w", "postalcode": 100000}
} print(id_dict["ChengDu"])#打印输出所有的值
print(id_dict["ChengDu"]["acreage"]) #打印输出成都的面积
id_dict["ChengDu"]["acreage"] = "面 积:14312平方千米" #给成都的面积重新修改为了"面 积:14312平方千米"
print(id_dict["ChengDu"]["acreage"])#打印 {'acreage': 14312, 'population': '1591.8w', 'postalcode': 610000}
14312
面 积:14312平方千米

将字典转换为元组:

print(id_dict.items())
dict_items([('stu1101', 'TengLan Wu'), ('stu1102', 'LongZe Luola'), ('stu1103', 'XiaoZe Maliya')])

将字典转换为列表:

list_test = list(id_dict)
print(list_test) ['stu1101', 'stu1102', 'stu1103']

字典的循环:

for key in id_dict:
print(key, id_dict[key])#由于加入了key,所以他把stu也循环出来了
输出:
stu1101 TengLan Wu
stu1102 LongZe Luola
stu1103 XiaoZe Maliya for key in id_dict:
print(id_dict[key])
输出:
TengLan Wu
LongZe Luola
XiaoZe Maliya

多字典嵌套的循环:

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# File_type:多字典嵌套
# Filename:dict_nest.py
# Author:smelond
id_dict = {
"ChengDu": {
"acreage": 14312, "population": "1591.8w", "postalcode": 610000},
"ShenZhen": {
"acreage": 1196, "population": "1190.08w", "postalcode": 518000},
"BeiJing": {
"acreage": "1.641w", "population": "2172.9w", "postalcode": 100000}
} for key in id_dict:#还是照常循环
test = id_dict[key]#将我们每次的key赋给test
for key in test:#再来循环test
# print(key, ":", test[key], "\n", end="")
print("%s : %s" % (key, test[key]))#将第二级的key打印,并且打印出value acreage : 14312
population : 1591.8w
postalcode : 610000
acreage : 1196
population : 1190.08w
postalcode : 518000
acreage : 1.641w
population : 2172.9w
postalcode : 100000

python字典的基本操作的更多相关文章

  1. Python 字典(Dictionary) 基本操作

    Python字典是一种可变容器模型,可存储任意类型对象:如字符串.数字.元组等.它以键值对(key-value)的形式存在,因此相当于Hashmap在python中的实现. §1. 创建字典  字典由 ...

  2. Python: 字典的基本操作

    字典是Python里唯一的映射类型.字典是可变的.无序的.大小可变的键值映射,有时候也称为散列表或关联数组. 例子在下面: dic = {"apple":2, "oran ...

  3. python字典的基本操作,以及可变数据类型和不可变数据类型的区分

    字典:采用键值对存储数据的数据类型,字典的键必须是不可变的数据类型 补充: 不可变(可哈希)数据类型:str,bool,int,tuple 可变(不可哈希)数据类型:list,  dict, set ...

  4. Python 字典方法

    访问字典的值 字典中的 键/值 实际上就是一种映射关系,只要知道了 “键”,就肯定知道 “值”. >>> my_dict = dict(name = 'zhangsan',other ...

  5. Python字典和集合

    Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...

  6. python 字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  7. python字典中的元素类型

    python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...

  8. python字典copy()方法

    python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...

  9. python 字典实现类似c的switch case

    #python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...

随机推荐

  1. mysql的mysqli异步与php的携程

    <?php $begin = time(); //同步请求 function multi_sync(){ $host = '192.168.2.87'; $user = 'census'; $p ...

  2. [android] 看博客学习hashCode()和equals()

    equals()是Object类提供的一个方法,众所周知,每一个java类都继承自Object,所以说每一个对象都有一个equals()方法,我们在用这个方法时却一般重写这个方法 Object类中eq ...

  3. 深入理解Java线程池:ThreadPoolExecutor

    线程池介绍 在web开发中,服务器需要接受并处理请求,所以会为一个请求来分配一个线程来进行处理.如果每次请求都新创建一个线程的话实现起来非常简便,但是存在一个问题: 如果并发的请求数量非常多,但每个线 ...

  4. Java基础——Servlet(二)

    好久没有写博客了,最近有在学习.可能是遇到瓶颈了,学到Servlet这里觉得有些吃力.前几天已经学完一遍了,但是学完之后觉得还是很迷茫.去知乎和百度上搜索,遇到的都是一些概念之类的讲解.核心的介绍说, ...

  5. 关于项目管理工具 maven

    众所周知,maven是目前很常用的项目管理工具.一般情况下,通过在pom.xml添加相应内容,再maven-->update就会自动把相应的jar包下载.配置好,非常方便. 一般每新建一个wor ...

  6. @ModelAttribute注解详解

    @ModelAttribute注解详解 1.@ModelAttribute定义: 被该注解定义的方法,会在该方法所在的controller的任何目标方法执行之前执行 2.@ModelAttribute ...

  7. js中的json的小例子

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  8. 消息推送SignalR简单实例

    消息推送SignalR:一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信. 功能:当所连接的客户端变得可用时服务器代码可以立即向其推送内容,而不是让服务器等待客户端请 ...

  9. CSS图标文字不对齐

    页面排版经常遇到‘图标+文字’的需求,正常样式写下来是这样 ​, 但产品要的应该是长这样 ​,怎么办呢?其实很简单,加个样式看看 vertical-align: top/middle/bottom; ...

  10. bzoj1758Wc10重建计划——solution

    1758: [Wc2010]重建计划 Time Limit: 40 Sec  Memory Limit: 162 MBSubmit: 4707  Solved: 1200[Submit][Status ...