字典的基本方法

什么是字典:

字典是一种 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. CentOS 7下搭配简单的C语言开发环境

    在CentOS 7下安装gcc,gcc是编译和运行C语言的工具, 安装命令: yum install gcc 中途如果有询问则输入y 安装成功后,通过以下命令 gcc --version 来查看安装的 ...

  2. 非Spring环境下使用Mybatis操作数据库的流程

    准备工作 1,  导入mybatis-3.2.7.jar,mysql-connector-5.1.25-bin.jar两个jar包 2,  在数据库中创建一个db_test数据库,库中有一个表为use ...

  3. C++函数中返回引用和返回值的区别

    一.主要讨论下面两个函数的区别: int& at() { return m_data_; } int at() { return m_data_; } 上面两个函数,第一个返回值是int的引用 ...

  4. Android so 文件进阶<二> 从dlsym()源码看android 动态链接过程

    0x00  前言 这篇文章其实是我之前学习elf文件关于符号表的学习笔记,网上也有很多关于符号表的文章,怎么说呢,感觉像是在翻译elf文件格式的文档一样,千篇一律,因此把自己的学习笔记分享出来.dls ...

  5. Python中bisect的使用

    在<Think Python>中第十章的练习中,涉及到了分半查找的bisect模块.为此,在网上查阅了Python中bisect模块的相关内容.有几个链接相对权威和明白: 1> ht ...

  6. es6学习笔记3--解构和对象

    1.解构 在接收数据的地方(比如赋值的左边),解构使你使用模式去获取部分数据. 下面的代码是解构的一个例子: let obj = { first: 'Jane', last: 'Doe' }; let ...

  7. [android] 新闻客户端引入SlidingMenu

    下载SlidingMenu,https://github.com/jfeinstein10/SlidingMenu 导入library 我们项目右键==>Properties==>Andr ...

  8. [Redis]Redis的数据类型

    存储String字符串,使用get,set命令,一个键最大存储512M 存储Hash哈希,使用HMSET和HGETALL命令,参数:键,值 例如:HMSET user:1 username taosh ...

  9. 一个人的旅行(hdu2066)Dijkstra算法模版

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  10. EF DataFirst修改数据类型

    在做软件的时候我们可能会遇到这样的问题,就是在使用EF的时候,有时候精度不一样, 我们用整数来计算肯定是比浮点数来得快的,但我在MySQL里面存储的数据类型是decimal的,我生成EF后, 里面的数 ...