在线客服
扫描二维码
下载博学谷APP扫描二维码
关注博学谷微信公众号
对于许多Python初学者来讲,数据结构中的字典是一个不容易理解的概念。字典作为一种容器型数据结构,它也可以算得上是最有用的容器。下面是小编整理的相关学习笔记,让我们一起来好好地学习有关于字典的语法知识吧~
1、字典的概念
在Python 中,字典这种数据类型的英文叫做 “dict”,有的语言里它的名称是 “hash”。字典是编程中最常用的数据结构之一。它是用来做映射或者存储你需要的键值对,这样当你需要的时候,你可以通过key来获取它的值。同样,程序员不会使用一个像“字典”这样的术语,来称呼那些不能像一个写满词汇的真实字典正常使用的事物,所以我们只要把它当做真实世界中的字典来用就好。
2、字典与列表的区别
针对列表你可以做这样的事情:
>>> things = ['a', 'b', 'c', 'd']
>>> print things[1]
b >>> things[1] =
'
z'
>>> print things[1]
z >>> things
['a', 'z', 'c', 'd']
你可以使用数字作为列表的索引,也就是你可以通过数字找到列表中的元素。你现在应该了解列表的这些特性,而你也应了解,你也只能通过数字来获取列表中的元素。而 dict是让你可以通过任何东西找到元素,不只是数字。是的,字典可以将一个物件和另外一个东西关联,不管它们的类型是什么,我们来看看:
>>> stuff = {'name': 'Zed', 'age': 39, 'height': 6 * 12 + 2}
>>> print stuff['name']
Zed
>>> print stuff['age']
39
>>> print stuff['height']
74
>>> stuff['city'] = "San Francisco"
>>> print stuff['city']
San Francisco
3、字典的使用
除了通过数字以外,我们还可以用字符串来从字典中获取 stuff ,我们还可以用字符串来往字典中添加元素。当然它支持的不只有字符串,我们还可以做这样的事情:
>>> stuff[1] = "Wow"
>>> stuff[2] = "Neato"
>>> print stuff[1]
Wow
>>> print stuff[2]
Neato
>>> stuff
{'city': 'San Francisco', 2: 'Neato', 'name': 'Zed', 1: 'Wow', 'age': 39, 'height': 74
}
这段代码中,使用了数字,当打印stuff的时候,不止有数字还有字符串作为字典的key。当然了,一个只能放东西进去的字典是没啥意思的,所以我们还要有删除的方法,也就是使用del这个关键字:
>>> del stuff['city']
>>> del stuff[1]
>>> del stuff[2]
>>> stuff
{'name': 'Zed', 'age': 36, 'height': 74}
4、实例练习
# create a mapping of state to abbreviation
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
}
# create a basic set of states and some cities in them
cities = {
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
}
# add some more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
# print out some cities
print '-' * 10
print "NY State has: ", cities['NY']
print "OR State has: ", cities['OR']
# print some states
print '-' * 10
print "Michigan's abbreviation is: ", states['Michigan']
print "Florida's abbreviation is: ", states['Florida']
# do it by using the state then cities dict
print '-' * 10
print "Michigan has: ", cities[states['Michigan']]
print "Florida has: ", cities[states['Florida']]
# print every state abbreviation
print '-' * 10
for state, abbrev in states.items():
print "%s is abbreviated %s" % (state, abbrev)
# print every city in state
print '-' * 10
for abbrev, city in cities.items():
print "%s has the city %s" % (abbrev, city)
# now do both at the same time
print '-' * 10
for state, abbrev in states.items():
print "%s state is abbreviated %s and has city %s" % (
state, abbrev, cities[abbrev])
print '-' * 10
# safely get a abbreviation by state that might not be there
state = states.get('Texas')
if not state:
print "Sorry, no Texas."
# get a city with a default value
city = cities.get('TX', 'Does Not Exist')
print "The city for the state 'TX' is: %s" % city
你看到的结果:
$ python ex39.py
----------
NY State has: New York
OR State has: Portland
----------
Michigan's abbreviation is: MI
Florida's abbreviation is: FL
----------
Michigan has: Detroit
Florida has: Jacksonville
----------
California is abbreviated CA
Michigan is abbreviated MI
New York is abbreviated NY
Florida is abbreviated FL
Oregon is abbreviated OR
----------
FL has the city Jacksonville
CA has the city San Francisco
MI has the city Detroit
OR has the city Portland
NY has the city New York
----------
California state is abbreviated CA and has city San Francisco
Michigan state is abbreviated MI and has city Detroit
New York state is abbreviated NY and has city New York
Florida state is abbreviated FL and has city Jacksonville
Oregon state is abbreviated OR and has city Portland
----------
Sorry, no Texas.
The city for the state 'TX' is: Does Not Exist
以上就是Python数据结构之字典的学习笔记,大家都理解了吗?事实上,当你需要通过一个值来访问另一个值的时候,就可以使用字典。关于字典的内容就到这里了,欢迎大家关注博学谷资讯栏目,我们将每天更新Python的学习内容~
— 申请免费试学名额 —
在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!
相关推荐 更多
想学Python这些问题必须了解
在人工智能以及数据科学越来越普及的互联网环境下,Python逐渐成为炙手可热的编程语言,由于Python入门简单、就业范围广泛、薪资水平较高等诸多优势,越来越多的朋友选择通过学习Python进入互联网领域。但是入门Python前很多核心的问题你了解吗?下面小编就为大家介绍一下想学Python必须了解的问题。
4121
2019-10-30 18:51:57
Python字符串常用技巧整理
相信在大家日常的文本处理工作中,都免不了要使用字符串。众所周知,使用Python处理字符串,十分的便捷和高效。本文整理了六大处理字符串的技巧,分别是字符串的切片和相乘,字符串中去掉一些字符,字符串的连接和合并,字符串的分割,字符串的开头和结尾的处理,字符串的查找和匹配。想要玩转Python字符串,就和我一起来看看吧!
4306
2019-12-02 16:44:31
了解Python五大特性提高工作效率
Python作为近些年来新崛起的编程语言黑马,发展可以说是势如破竹,风头强劲。而随着Python一起升温的,还有全民学习Python的热度。虽说Python是新手友好的语言,但对于零基础的初学者而言,学习一门编程语言的难度并不低。为了帮助大家更好的掌握Python,本文将为大家总结归纳可以提高工作效率的Python五大特性,分别是列表操作、压缩和枚举、列表推导式、虚拟环境的使用和生成器。
4145
2020-01-11 19:26:10
表单标签有哪些?分别做什么?
表单用于搜集不同类型的用户输入,然后把用户数据提交到服务器后台,表单开发中用到了很多标签,如<form>标签 、<label>标签、<input>、<textarea> 标签、<select> 标签等,接下来我们一起来学习下。
11545
2021-12-13 15:10:25
MySQL数据库的基本使用之数据类型和约束
MySQL数据库的基本使用之数据类型和约束,数据类型包含整数类型、浮点数、字符串、日期等。储存在数据库中的所有数据值均为正确的状态,如有不正确数据则称为已丧失数据完整性。
2375
2022-01-11 10:50:33