您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Python中字典有什么用”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“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中字典有什么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。