Python中字典有什么用-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Python中字典有什么用

这篇文章主要为大家展示了“Python中字典有什么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Python中字典有什么用”这篇文章吧。

创新互联-专业网站定制、快速模板网站建设、高性价比龙胜网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式龙胜网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖龙胜地区。费用合理售后完善,十余年实体公司更值得信赖。

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中字典有什么用
网址分享:http://kswsj.cn/article/ispojs.html

其他资讯