字典的基本方法

什么是字典:

字典是一种 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 Json类型的数据处理

    新建表 CREATE TABLE `user_copy` ( `id` ) NOT NULL, `name` ) DEFAULT NULL, `lastlogininfo` json DEFAULT ...

  2. sql server数据库查看锁表和解锁

    --查看锁表: select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys ...

  3. RocketMQ专题1:入门

    RocketMQ入门 源码和应用下载 ​ 这里以RocketMQ的4.3.0版本为例,本地环境为windows10,jdk1.8, maven3.2.1. 源码下载地址: http://mirrors ...

  4. C# Azure 设置云端应用程序的默认时间

    在微软云Azure中,应用程序(website)的默认时间是按照美国UTC的时间的. 例如,在应用程序中获取DateTime.Now,的时候,是获取UTC的时间,不是中国的时间. 所以我们开始在这里设 ...

  5. 在window环境下挂载mysql数据卷

    1.提前在指定的目录下创建一个my.cnf文件,目录名最好为英文且不带特殊符号和空格,文件内容如下,注意:粘贴时要把每一行末尾的空格去除,否则运行时会报错说utf8编码错误 [mysqld] user ...

  6. ASP.NET MVC加载用户控件后并获取其内控件值或赋值

    有网友看了这篇<ASP.NET MVC加载ASCX之后,并为之赋值>http://www.cnblogs.com/insus/p/3643254.html 之后,问及Insus.NET,不 ...

  7. 逆向工程生成的mybatis中mapper文件。mapper接口,实例化成对象

    逆向工程生成的mybatis中mapper文件中,*mapper文件只是接口,而不是类文件.但是却可以通过spring的容器获得实例. 例如: //1.获得mapper代理对象,从spring容器获得 ...

  8. RocketMQ 概述

    Rocket 火箭 MQ的作用:同步转异步(异步解耦). 难点:如何确保消息一定被消费,而且仅消费一次. 1.消息架构:生产者.服务器.消费者.路由发现. 2.消息顺序:严格按照消息到达服务器的顺序进 ...

  9. python3.8 新特性

    https://docs.python.org/3.8/whatsnew/3.8.html python 3.8的新功能本文解释了与3.7相比,python 3.8中的新特性. 有关完整的详细信息,请 ...

  10. JQ面试问题(转载)

    1 你在公司是怎么用jquery的? 答:在项目中是怎么用的是看看你有没有项目经验(根据自己的实际情况来回答) 你用过的选择器啊,动画啊,表单啊,ajax事件等 配置Jquery环境 下载jquery ...