用 Jupyter 学习 Python 字典
Python Jupyter 字典

> 
> 字典数据结构可以帮助你快速访问信息。
> 
> 
> 


![](/data/attachment/album/202103/26/094720i58u5qxx3l4qsssx.jpg "Hands on a keyboard with a Python book ")


字典是 Python 编程语言使用的数据结构。一个 Python 字典由多个键值对组成；每个键值对将键映射到其关联的值上。


例如你是一名老师，想把学生姓名与成绩对应起来。你可以使用 Python 字典，将学生姓名映射到他们关联的成绩上。此时，键值对中键是姓名，值是对应的成绩。


如果你想知道某个学生的考试成绩，你可以从字典中访问。这种快捷查询方式可以为你节省解析整个列表找到学生成绩的时间。


本文介绍了如何通过键访问对应的字典值。学习前，请确保你已经安装了 [Anaconda 包管理器](https://docs.anaconda.com/anaconda/)和 [Jupyter 笔记本](https://opensource.com/article/18/3/getting-started-jupyter-notebooks)。


### 1、在 Jupyter 中打开一个新的笔记本


首先在 Web 浏览器中打开并运行 Jupyter。然后，


1. 转到左上角的 “File”。
2. 选择 “New Notebook”，点击 “Python 3”。


![新建 Jupyter 笔记本](/data/attachment/album/202103/26/094801rl4pik51nd34qz5l.png "Create Jupyter notebook")


开始时，新建的笔记本是无标题的，你可以将其重命名为任何名称。我为我的笔记本取名为 “[OpenSource.com](http://OpenSource.com) Data Dictionary Tutorial”。


笔记本中标有行号的位置就是你写代码的区域，也是你输入的位置。


在 macOS 上，可以同时按 `Shift + Return` 键得到输出。在创建新的代码区域前，请确保完成上述动作；否则，你写的任何附加代码可能无法运行。


### 2、新建一个键值对


在字典中输入你希望访问的键与值。输入前，你需要在字典上下文中定义它们的含义：



```
empty_dictionary = {}
grades = {
    "Kelsey": 87,
    "Finley": 92
}

one_line = {a: 1, b: 2}

```

![定义字典键值对的代码](/data/attachment/album/202103/26/094801k6jqezjqaozqwwzs.png "Code for defining key-value pairs in the dictionary")


这段代码让字典将特定键与其各自的值关联起来。字典按名称存储数据，从而可以更快地查询。


### 3、通过键访问字典值


现在你想查询指定的字典值；在上述例子中，字典值指特定学生的成绩。首先，点击 “Insert” 后选择 “Insert Cell Below”。


![在 Jupyter 插入新建单元格](/data/attachment/album/202103/26/094801xdfp9f4wee4uf4de.png "Inserting a new cell in Jupyter")


在新单元格中，定义字典中的键与值。


然后，告诉字典打印该值的键，找到需要的值。例如，查询名为 Kelsey 的学生的成绩：



```
# 访问字典中的数据
grades = {
    "Kelsey": 87,
    "Finley": 92
}

print(grades["Kelsey"])
87

```

![查询特定值的代码](/data/attachment/album/202103/26/094802my6m8070sky861lx.png "Code to look for a specific value")


当你查询 Kelsey 的成绩（也就是你想要查询的值）时，如果你用的是 macOS，只需要同时按 `Shift+Return` 键。


你会在单元格下方看到 Kelsey 的成绩。


### 4、更新已有的键


当把一位学生的错误成绩添加到字典时，你会怎么办？可以通过更新字典、存储新值来修正这类错误。


首先，选择你想更新的那个键。在上述例子中，假设你错误地输入了 Finley 的成绩，那么 Finley 就是你需要更新的键。


为了更新 Finley 的成绩，你需要在下方插入新的单元格，然后创建一个新的键值对。同时按 `Shift+Return` 键打印字典全部信息：



```
grades["Finley"] = 90
print(grades)

{'Kelsey': 87; "Finley": 90}

```

![更新键的代码](/data/attachment/album/202103/26/094802rg6ci3culko3ligg.png "Code for updating a key")


单元格下方输出带有 Finley 更新成绩的字典。


### 5、添加新键


假设你得到一位新学生的考试成绩。你可以用新键值对将那名学生的姓名与成绩补充到字典中。


插入新的单元格，以键值对形式添加新学生的姓名与成绩。当你完成这些后，同时按 `Shift+Return` 键打印字典全部信息：



```
grades["Alex"] = 88
print(grades)

{'Kelsey': 87, 'Finley': 90, 'Alex': 88}

```

![添加新键](/data/attachment/album/202103/26/094802q4fk427lbekv4ukx.png "Add a new key")


所有的键值对输出在单元格下方。


### 使用字典


请记住，键与值可以是任意数据类型，但它们很少是<ruby> <a href="https://www.datacamp.com/community/tutorials/data-structures-python">  扩展数据类型 </a> <rt>  non-primitive types </rt></ruby>。此外，字典不能以指定的顺序存储、组织里面的数据。如果你想要数据有序，最好使用 Python 列表，而非字典。


如果你考虑使用字典，首先要确认你的数据结构是否是合适的，例如像电话簿的结构。如果不是，列表、元组、树或者其他数据结构可能是更好的选择。




---


via: <https://opensource.com/article/21/3/dictionary-values-python>


作者：[Lauren Maffeo](https://opensource.com/users/lmaffeo) 选题：[lujun9972](https://github.com/lujun9972) 译者：[DCOLIVERSUN](https://github.com/DCOLIVERSUN) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Dictionaries are the Python programming language's way of implementing data structures. A Python dictionary consists of several key-value pairs; each pair maps the key to its associated value.

For example, say you're a teacher who wants to match students' names to their grades. You could use a Python dictionary to map the keys (names) to their associated values (grades).

If you need to find a specific student's grade on an exam, you can access it from your dictionary. This lookup shortcut should save you time over parsing an entire list to find the student's grade.

This article shows you how to access dictionary values through each value's key. Before you begin the tutorial, make sure you have the [Anaconda package manager](https://docs.anaconda.com/anaconda/) and [Jupyter Notebook](https://opensource.com/article/18/3/getting-started-jupyter-notebooks) installed on your machine.

## 1. Open a new notebook in Jupyter

Begin by opening Jupyter and running it in a tab in your web browser. Then:

- Go to
**File**in the top-left corner. - Select
**New Notebook**, then**Python 3**.

![Create Jupyter notebook Create Jupyter notebook](https://opensource.com/sites/default/files/uploads/new-jupyter-notebook.png)

(Lauren Maffeo, CC BY-SA 4.0)


Your new notebook starts off untitled, but you can rename it anything you'd like. I named mine **OpenSource.com Data Dictionary Tutorial**.

The line number you see in your new Jupyter notebook is where you will write your code. (That is, your input.)

On macOS, you'll hit **Shift** then **Return** to receive your output. Make sure to do this before creating new line numbers; otherwise, any additional code you write might not run.

## 2. Create a key-value pair

Write the keys and values you wish to access in your dictionary. To start, you'll need to define what they are in the context of your dictionary:

```
empty_dictionary = {}
grades = {
"Kelsey": 87,
"Finley": 92
}
one_line = {a: 1, b: 2}
```



![Defining key-value pairs in the dictionary Code for defining key-value pairs in the dictionary](https://opensource.com/sites/default/files/uploads/define-keys-values.png)

(Lauren Maffeo, CC BY-SA 4.0)

This allows the dictionary to associate specific keys with their respective values. Dictionaries store data by name, which allows faster lookup.

## 3. Access a dictionary value by its key

Say you want to find a specific dictionary value; in this case, a specific student's grade. To start, hit **Insert** then **Insert Cell Below**.

![Inserting a new cell in Jupyter Inserting a new cell in Jupyter](https://opensource.com/sites/default/files/uploads/jupyter_insertcell.png)

(Lauren Maffeo, CC BY-SA 4.0)

In your new cell, define the keys and values in your dictionary.

Then, find the value you need by telling your dictionary to print that value's key. For example, look for a specific student's name—Kelsey:

```
# Access data in a dictionary
grades = {
"Kelsey": 87,
"Finley": 92
}
print(grades["Kelsey"])
87
```



![Code to look for a specific value Code to look for a specific value](https://opensource.com/sites/default/files/uploads/lookforvalue.png)

(Lauren Maffeo, CC BY-SA 4.0)

Once you've asked for Kelsey's grade (that is, the value you're trying to find), hit **Shift** (if you're on macOS), then **Return**.

You see your desired value—Kelsey's grade—as an output below your cell.

## 4. Update an existing key

What if you realize you added the wrong grade for a student to your dictionary? You can fix it by updating your dictionary to store an additional value.

To start, choose which key you want to update. In this case, say you entered Finley's grade incorrectly. That is the key you'll update in this example.

To update Finley's grade, insert a new cell below, then create a new key-value pair. Tell your cell to print the dictionary, then hit **Shift** and **Return**:

```
grades["Finley"] = 90
print(grades)
{'Kelsey': 87; "Finley": 90}
```



![Code for updating a key Code for updating a key](https://opensource.com/sites/default/files/uploads/jupyter_updatekey.png)

(Lauren Maffeo, CC BY-SA 4.0)

The updated dictionary, with Finley's new grade, appears as your output.

## 5. Add a new key

Say you get a new student's grade for an exam. You can add that student's name and grade to your dictionary by adding a new key-value pair.

Insert a new cell below, then add the new student's name and grade as a key-value pair. Once you're done, tell your cell to print the dictionary, then hit **Shift** and **Return**:

```
grades["Alex"] = 88
print(grades)
{'Kelsey': 87, 'Finley': 90, 'Alex': 88}
```



![Add a new key Add a new key](https://opensource.com/sites/default/files/uploads/jupyter_addnewkey.png)

(Lauren Maffeo, CC BY-SA 4.0)

All key-value pairs should appear as output.

## Using dictionaries

Remember that keys and values can be any data type, but it's rare for them to be [non-primitive types](https://www.datacamp.com/community/tutorials/data-structures-python). Additionally, dictionaries don't store or structure their content in any specific order. If you need an ordered sequence of items, it's best to create a list in Python, not a dictionary.

If you're thinking of using a dictionary, first confirm if your data is structured the right way, i.e., like a phone book. If not, then using a list, tuple, tree, or other data structure might be the best option.

## 1 Comment