markdown 5. Python - 字典
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 5. Python - 字典相关的知识,希望对你有一定的参考价值。
<h1 align=center><font size = 5>Sets and Dictionaries</font></h1>
## Table of Contents
<div class="alert alert-block alert-info" style="margin-top: 20px">
<li><a href="#ref2">Dictionaries</a></li>
<br>
<p></p>
Estimated Time Needed: <strong>20 min</strong>
</div>
<hr>
<a id="ref2"></a>
<h2 align=center> Dictionaries in Python </h2>
A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of the numerical indexes such as a list, dictionaries have keys. These keys are labels that are used to access values within a dictionary.
<a ><img src = "https://ibm.box.com/shared/static/6tyznuwydogmtuv73o8l5g7xsb8o92h2.png" width = 650, align = "center"></a>
<h4 align=center> A Comparison of a Dictionary to a list: Instead of the numerical indexes like a list, dictionaries have keys.
</h4>
An example of a Dictionary **Dict**:
```python
Dict={"key1":1,"key2":"2","key3":[3,3,3],"key4":(4,4,4),('key5'):5,(0,1):6}
Dict
```
{'key1': 1,
'key2': '2',
'key3': [3, 3, 3],
'key4': (4, 4, 4),
'key5': 5,
(0, 1): 6}
The keys can be strings:
```python
Dict["key1"]
```
1
Keys can also be any immutable object such as a tuple:
```python
Dict[(0,1)]
```
6
Each key is separated from its value by a colon "**:**". Commas separate the items, and the whole dictionary is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this "**{}**".
```python
release_year_dict = {"Thriller":"1982", "Back in Black":"1980", \
"The Dark Side of the Moon":"1973", "The Bodyguard":"1992", \
"Bat Out of Hell":"1977", "Their Greatest Hits (1971-1975)":"1976", \
"Saturday Night Fever":"1977", "Rumours":"1977"}
release_year_dict
```
{'Thriller': '1982',
'Back in Black': '1980',
'The Dark Side of the Moon': '1973',
'The Bodyguard': '1992',
'Bat Out of Hell': '1977',
'Their Greatest Hits (1971-1975)': '1976',
'Saturday Night Fever': '1977',
'Rumours': '1977'}
In summary, like a list, a dictionary holds a sequence of elements. Each element is represented by a key and its corresponding value. Dictionaries are created with two curly braces containing keys and values separated by a colon. For every key, there can only be a single value, however, multiple keys can hold the same value. Keys can only be strings, numbers, or tuples, but values can be any data type.
It is helpful to visualize the dictionary as a table, as in figure 9. The first column represents the keys, the second column represents the values.
<a ><img src = "https://ibm.box.com/shared/static/i45fppou18c3t0fuf2ikks48tod7chbl.png" width = 650, align = "center"></a>
<h4 align=center> Figure 9: Table representing a Dictionary
</h4>
You will need this dictionary for the next two questions :
```python
soundtrack_dic = { "The Bodyguard":"1992", "Saturday Night Fever":"1977"}
soundtrack_dic
```
{'The Bodyguard': '1992', 'Saturday Night Fever': '1977'}
#### In the dictionary "soundtrack_dict" what are the keys ?
```python
soundtrack_dic.keys()
```
dict_keys(['The Bodyguard', 'Saturday Night Fever'])
<div align="right">
<a href="#Dict1" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="Dict1" class="collapse">
```
The Keys "The Bodyguard" and "Saturday Night Fever"
```
</div>
#### In the dictionary "soundtrack_dict" what are the values ?
<div align="right">
<a href="#Dict2" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="Dict2" class="collapse">
```
The values are "1992" and "1977"
```
</div>
You can retrieve the values based on the names:
```python
release_year_dict['Thriller']
```
'1982'
This corresponds to:
<a ><img src = "https://ibm.box.com/shared/static/glbwz23cgjjxqi7rjxn7me5i16gan7h7.png" width = 500, align = "center"></a>
<h4 align=center>
Table used to represent accessing the value for "Thriller"
</h4>
Similarly for The Bodyguard
```python
release_year_dict['The Bodyguard']
```
'1992'
<a ><img src = "https://ibm.box.com/shared/static/6t7bu8jusckaskukwq1k0a3im5ltcpsn.png " width = 500, align = "center"></a>
<h4 align=center>
Accessing the value for the "The Bodyguard"
</h4>
Now let us retrieve the keys of the dictionary using the method **release_year_dict()**:
```python
release_year_dict.keys()
```
dict_keys(['Thriller', 'Back in Black', 'The Dark Side of the Moon', 'The Bodyguard', 'Bat Out of Hell', 'Their Greatest Hits (1971-1975)', 'Saturday Night Fever', 'Rumours'])
You can retrieve the values using the method **`values()`**:
```python
release_year_dict.values()
```
dict_values(['1982', '1980', '1973', '1992', '1977', '1976', '1977', '1977'])
We can add an entry:
```python
release_year_dict['Graduation']='2007'
release_year_dict
```
{'Thriller': '1982',
'Back in Black': '1980',
'The Dark Side of the Moon': '1973',
'The Bodyguard': '1992',
'Bat Out of Hell': '1977',
'Their Greatest Hits (1971-1975)': '1976',
'Saturday Night Fever': '1977',
'Rumours': '1977',
'Graduation': '2007'}
We can delete an entry:
```python
del(release_year_dict['Thriller'])
del(release_year_dict['Graduation'])
release_year_dict
```
{'Back in Black': '1980',
'The Dark Side of the Moon': '1973',
'The Bodyguard': '1992',
'Bat Out of Hell': '1977',
'Their Greatest Hits (1971-1975)': '1976',
'Saturday Night Fever': '1977',
'Rumours': '1977'}
We can verify if an element is in the dictionary:
```python
'The Bodyguard' in release_year_dict
```
True
#### The Albums 'Back in Black', 'The Bodyguard' and 'Thriller' have the following music recording sales in millions 50, 50 and 65 respectively:
##### a) Create a dictionary “album_sales_dict” where the keys are the album name and the sales in millions are the values.
```python
album_sales_dict = {'Back in Black':50, 'The Bodyguard':50, 'Thriller':65}
album_sales_dict
```
{'Back in Black': 50, 'The Bodyguard': 50, 'Thriller': 65}
<div align="right">
<a href="#q9" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="q9" class="collapse">
```
album_sales_dict= { "The Bodyguard":50, "Back in Black":50,"Thriller":65}
```
</div>
#### b) Use the dictionary to find the total sales of "Thriller":
```python
album_sales_dict['Thriller']
```
65
<div align="right">
<a href="#q10b" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="q10b" class="collapse">
```
album_sales_dict["Thriller"]
```
</div>
#### c) Find the names of the albums from the dictionary using the method "keys":
```python
album_sales_dict.keys()
```
dict_keys(['Back in Black', 'The Bodyguard', 'Thriller'])
<div align="right">
<a href="#q10c" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="q10c" class="collapse">
```
album_sales_dict.keys()
```
#### d) Find the names of the recording sales from the dictionary using the method "values":
```python
album_sales_dict.values()
```
dict_values([50, 50, 65])
<div align="right">
<a href="#q10d" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="q10d" class="collapse">
```
album_sales_dict.values()
```
</div>
<a href="http://cocl.us/bottemNotebooksPython101Coursera"><img src = "https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png" width = 750, align = "center"></a>
# About the Authors:
[Joseph Santarcangelo]( https://www.linkedin.com/in/joseph-s-50398b136/) has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.
<hr>
Copyright © 2017 [cognitiveclass.ai](cognitiveclass.ai?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/).
以上是关于markdown 5. Python - 字典的主要内容,如果未能解决你的问题,请参考以下文章
Python3基础 dict items 以元组的形式打印出字典的每一个项