Merge pull request #2 from LCTT/master

update from lctt
This commit is contained in:
Qian.Sun 2021-03-23 22:26:06 +08:00 committed by GitHub
commit 9676c9e583
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 269 additions and 279 deletions

View File

@ -0,0 +1,103 @@
[#]: collector: (lujun9972)
[#]: translator: (ShuyRoy)
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-13229-1.html)
[#]: subject: (Get started with distributed tracing using Grafana Tempo)
[#]: via: (https://opensource.com/article/21/2/tempo-distributed-tracing)
[#]: author: (Annanay Agarwal https://opensource.com/users/annanayagarwal)
使用 Grafana Tempo 进行分布式跟踪
======
> Grafana Tempo 是一个新的开源、大容量分布式跟踪后端。
![](https://img.linux.net.cn/data/attachment/album/202103/23/221354lc1eiill7lln4lli.jpg)
Grafana 的 [Tempo][2] 是出自 Grafana 实验室的一个简单易用、大规模的、分布式的跟踪后端。Tempo 集成了 [Grafana][3]、[Prometheus][4] 以及 [Loki][5],并且它只需要对象存储进行操作,因此成本低廉,操作简单。
我从一开始就参与了这个开源项目,所以我将介绍一些关于 Tempo 的基础知识,并说明为什么云原生社区会注意到它。
### 分布式跟踪
想要收集对应用程序请求的遥测数据是很常见的。但是在现在的服务器中,单个应用通常被分割为多个微服务,可能运行在几个不同的节点上。
分布式跟踪是一种获得关于应用的性能细粒度信息的方式该应用程序可能由离散的服务组成。当请求到达一个应用时它提供了该请求的生命周期的统一视图。Tempo 的分布式跟踪可以用于单体应用或微服务应用,它提供 [请求范围的信息][6],使其成为可观察性的第三个支柱(另外两个是度量和日志)。
接下来是一个分布式跟踪系统生成应用程序甘特图的示例。它使用 Jaeger [HotROD][7] 的演示应用生成跟踪,并把它们存到 Grafana 云托管的 Tempo 上。这个图展示了按照服务和功能划分的请求处理时间。
![Gantt chart from Grafana Tempo][8]
### 减少索引的大小
在丰富且定义良好的数据模型中,跟踪包含大量信息。通常,跟踪后端有两种交互:使用元数据选择器(如服务名或者持续时间)筛选跟踪,以及筛选后的可视化跟踪。
为了加强搜索,大多数的开源分布式跟踪框架会对跟踪中的许多字段进行索引,包括服务名称、操作名称、标记和持续时间。这会导致索引很大,并迫使你使用 Elasticsearch 或者 [Cassandra][10] 这样的数据库。但是,这些很难管理,而且大规模运营成本很高,所以我在 Grafana 实验室的团队开始提出一个更好的解决方案。
在 Grafana 中,我们的待命调试工作流从使用指标报表开始(我们使用 [Cortex][11] 来存储我们应用中的指标,它是一个云原生基金会孵化的项目,用于扩展 Prometheus深入研究这个问题筛选有问题服务的日志我们将日志存储在 Loki 中,它就像 Prometheus 一样,只不过 Loki 是存日志的),然后查看跟踪给定的请求。我们意识到,我们过滤时所需的所有索引信息都可以在 Cortex 和 Loki 中找到。但是,我们需要一个强大的集成,以通过这些工具实现跟踪的可发现性,并需要一个很赞的存储,以根据跟踪 ID 进行键值查找。
这就是 [Grafana Tempo][12] 项目的开始。通过专注于给定检索跟踪 ID 的跟踪,我们将 Tempo 设计为最小依赖性、大容量、低成本的分布式跟踪后端。
### 操作简单,性价比高
Tempo 使用对象存储后端,这是它唯一的依赖。它既可以被用于单一的二进制下,也可以用于微服务模式(请参考仓库中的 [例子][13],了解如何轻松上手)。使用对象存储还意味着你可以存储大量的应用程序的痕迹,而无需任何采样。这可以确保你永远不会丢弃那百万分之一的出错或具有较高延迟的请求的跟踪。
### 与开源工具的强大集成
[Grafana 7.3 包括了 Tempo 数据源][14],这意味着你可以在 Grafana UI 中可视化来自Tempo 的跟踪。而且,[Loki 2.0 的新查询特性][15] 使得 Tempo 中的跟踪更简单。为了与 Prometheus 集成,该团队正在添加对<ruby>范例<rt>exemplar</rt></ruby>的支持,范例是可以添加到时间序列数据中的高基数元数据信息。度量存储后端不会对它们建立索引,但是你可以在 Grafana UI 中检索和显示度量值。尽管范例可以存储各种元数据,但是在这个用例中,存储跟踪 ID 是为了与 Tempo 紧密集成。
这个例子展示了使用带有请求延迟直方图的范例,其中每个范例数据点都链接到 Tempo 中的一个跟踪。
![Using exemplars in Tempo][16]
### 元数据一致性
作为容器化应用程序运行的应用发出的遥测数据通常具有一些相关的元数据。这可以包括集群 ID、命名空间、吊舱 IP 等。这对于提供基于需求的信息是好的,但如果你能将元数据中包含的信息用于生产性的东西,那就更好了。
 
例如,你可以使用 [Grafana 云代理将跟踪信息导入 Tempo 中][17],代理利用 Prometheus 服务发现机制轮询 Kubernetes API 以获取元数据信息,并且将这些标记添加到应用程序发出的跨域数据中。由于这些元数据也在 Loki 中也建立了索引,所以通过元数据转换为 Loki 标签选择器,可以很容易地从跟踪跳转到查看给定服务的日志。
下面是一个一致元数据的示例它可用于Tempo跟踪中查看给定范围的日志。
![][18]
### 云原生
Grafana Tempo 可以作为容器化应用,你可以在如 Kubernetes、Mesos 等编排引擎上运行它。根据获取/查询路径上的工作负载各种服务可以水平伸缩。你还可以使用云原生的对象存储如谷歌云存储、Amazon S3 或者 Tempo Azure 博客存储。更多的信息,请阅读 Tempo 文档中的 [架构部分][19]。
### 试一试 Tempo
如果这对你和我们一样有用,可以 [克隆 Tempo 仓库][20]试一试。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/2/tempo-distributed-tracing
作者:[Annanay Agarwal][a]
选题:[lujun9972][b]
译者:[RiaXu](https://github.com/ShuyRoy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/annanayagarwal
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_space_graphic_cosmic.png?itok=wu493YbB (Computer laptop in space)
[2]: https://grafana.com/oss/tempo/
[3]: http://grafana.com/oss/grafana
[4]: https://prometheus.io/
[5]: https://grafana.com/oss/loki/
[6]: https://peter.bourgon.org/blog/2017/02/21/metrics-tracing-and-logging.html
[7]: https://github.com/jaegertracing/jaeger/tree/master/examples/hotrod
[8]: https://opensource.com/sites/default/files/uploads/tempo_gantt.png (Gantt chart from Grafana Tempo)
[9]: https://creativecommons.org/licenses/by-sa/4.0/
[10]: https://opensource.com/article/19/8/how-set-apache-cassandra-cluster
[11]: https://cortexmetrics.io/
[12]: http://github.com/grafana/tempo
[13]: https://grafana.com/docs/tempo/latest/getting-started/example-demo-app/
[14]: https://grafana.com/blog/2020/10/29/grafana-7.3-released-support-for-the-grafana-tempo-tracing-system-new-color-palettes-live-updates-for-dashboard-viewers-and-more/
[15]: https://grafana.com/blog/2020/11/09/trace-discovery-in-grafana-tempo-using-prometheus-exemplars-loki-2.0-queries-and-more/
[16]: https://opensource.com/sites/default/files/uploads/tempo_exemplar.png (Using exemplars in Tempo)
[17]: https://grafana.com/blog/2020/11/17/tracing-with-the-grafana-cloud-agent-and-grafana-tempo/
[18]: https://lh5.googleusercontent.com/vNqk-ygBOLjKJnCbTbf2P5iyU5Wjv2joR7W-oD7myaP73Mx0KArBI2CTrEDVi04GQHXAXecTUXdkMqKRq8icnXFJ7yWUEpaswB1AOU4wfUuADpRV8pttVtXvTpVVv8_OfnDINgfN
[19]: https://grafana.com/docs/tempo/latest/architecture/architecture/
[20]: https://github.com/grafana/tempo

View File

@ -1,167 +0,0 @@
[#]: subject: (Learn Python dictionary values with Jupyter)
[#]: via: (https://opensource.com/article/21/3/dictionary-values-python)
[#]: author: (Lauren Maffeo https://opensource.com/users/lmaffeo)
[#]: collector: (lujun9972)
[#]: translator: (DCOLIVERSUN)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
Learn Python dictionary values with Jupyter
======
Implementing data structures with dictionaries helps you access
information more quickly.
![Hands on a keyboard with a Python book ][1]
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][2] and [Jupyter Notebook][3] 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:
1. Go to **File** in the top-left corner.
2. Select **New Notebook**, then **Python 3**.
![Create Jupyter notebook][4]
(Lauren Maffeo, [CC BY-SA 4.0][5])
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}
```
![Code for defining key-value pairs in the dictionary][6]
(Lauren Maffeo, [CC BY-SA 4.0][5])
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][7]
(Lauren Maffeo, [CC BY-SA 4.0][5])
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][8]
(Lauren Maffeo, [CC BY-SA 4.0][5])
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][9]
(Lauren Maffeo, [CC BY-SA 4.0][5])
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][10]
(Lauren Maffeo, [CC BY-SA 4.0][5])
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][11]. 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.
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/3/dictionary-values-python
作者:[Lauren Maffeo][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/lmaffeo
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python-programming-code-keyboard.png?itok=fxiSpmnd (Hands on a keyboard with a Python book )
[2]: https://docs.anaconda.com/anaconda/
[3]: https://opensource.com/article/18/3/getting-started-jupyter-notebooks
[4]: https://opensource.com/sites/default/files/uploads/new-jupyter-notebook.png (Create Jupyter notebook)
[5]: https://creativecommons.org/licenses/by-sa/4.0/
[6]: https://opensource.com/sites/default/files/uploads/define-keys-values.png (Code for defining key-value pairs in the dictionary)
[7]: https://opensource.com/sites/default/files/uploads/jupyter_insertcell.png (Inserting a new cell in Jupyter)
[8]: https://opensource.com/sites/default/files/uploads/lookforvalue.png (Code to look for a specific value)
[9]: https://opensource.com/sites/default/files/uploads/jupyter_updatekey.png (Code for updating a key)
[10]: https://opensource.com/sites/default/files/uploads/jupyter_addnewkey.png (Add a new key)
[11]: https://www.datacamp.com/community/tutorials/data-structures-python

View File

@ -1,112 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (ShuyRoy )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Get started with distributed tracing using Grafana Tempo)
[#]: via: (https://opensource.com/article/21/2/tempo-distributed-tracing)
[#]: author: (Annanay Agarwal https://opensource.com/users/annanayagarwal)
使用Grafana Tempo开始分布式跟踪
======
Grafana Tempo是一个新的开源、大容量分布式跟踪后端。
![Computer laptop in space][1]
Grafana的[Tempo][2]是出自Grafana实验室的一个简单易用、大规模集成、分布式的跟踪后端。Tempo集成了[Grafana][3]、[Prometheus][4]以及[Loki][5],并且它只需要对象存储进行操作,这使得它是合算的且易操作的。
我从一开始就参与了这个开源项目所以我将介绍一些关于Tempo的基础知识并说明为什么本地云社区会注意到它。
### 分布式跟踪
想要收集对应用程序请求的遥测数据是很常见的。但是在现在的服务器中,单个应用通常被分割为多个微服务,可能运行在几个不同的节点上。
分布式跟踪是一种获得关于应用的性能细粒度信息的方式该应用程序可能由离散的服务组成。当请求到达一个应用时它提供了请求生命周期的统一视图。Tempo的分布式跟踪可以用于单片或微服务应用它提供[请求范围的信息][6],使其成为可观察的第三个支柱(除了度量和日志)。
接下来是一个分布式跟踪系统生成应用程序甘特图的示例。它使用Jaeger [HotROD][7] 的演示应用生成跟踪并把他们存到Grafana云托管的Tempo上。这个图展示了按照服务和功能划分的请求处理时间。
![Gantt chart from Grafana Tempo][8]
(Annanay Agarwal, [CC BY-SA 4.0][9])
### 减少索引的大小
在丰富且定义良好的数据模型中,跟踪包含大量信息。通常,跟踪后端有两种交互:使用元数据选择器(如服务名或者持续时间)筛选跟踪,并在筛选后可视化跟踪。
为了加强查找大多数的开源分布式跟踪框架对跟踪中的许多字段进行索引包括服务名称、操作名称、标记和持续时间。这会导致索引很大并迫使您使用Elasticsearch或者[Cassandra][10]这样的数据库。但是这些很难管理而且大规模操作的成本高所以我在Grafana实验室的团队打算提出一个更好的解决方案。
在Grafana中我的待命调试工作流开始使用指标报表我们使用[Cortex][11]来存储我们应用中的指标它是一个云本地计算基金会孵化的项目用于扩展Prometheus深入研究这个问题筛选有问题服务的日志我们将日志存储在Loki中就像Prometheus一样只不过Loki是存日志的然后查看跟踪给定的请求。我们意识到我们过滤时所需的所有索引信息都可以在Cortex和Loki中找到。但是我们需要通过这些工具实现跟踪可发现的强大集成以及根据跟踪ID进行键值查找的免费存储。
这是[Grafana Tempo][12]项目的开始。通过关注给定跟踪ID的跟踪检索我们将Tempo设计为最小依赖、高容量、低成本的分布式跟踪后端。
### 容易操作和低成本
Tempo使用对象存储后端这是它唯一的依赖。它既可以被用于单二进制模式下也可以用于微服务模式请参考repo中的[例子][13],了解如何轻松开始)。使用对象存储也意味着你可以在不使用任何抽样的情况下存储应用的的大量跟踪。这可以确保你永远不会丢弃出错或延迟更高的百万分之一的请求。
### 与开源工具的强大集成
[Grafana 7.3包括了Tempo数据源][14]这意味着你可以在Grafana UI中可视化来自Tempo的跟踪。而且[Loki 2.0的新查询特性][15]使得Tempo中的跟踪更简单。为了与Prometheus集成该团队正在添加对范例的支持范例是可以添加到时间序列数据中的高基数元数据信息。度量存储后端不会对它们建立索引但是你可以在Grafana UI中检索和显示度量值。尽管exemplars可以存储各种元数据但是在这个用例中跟踪的ID被存储以便与Tempo强集成。
这个例子展示了使用带有请求延迟直方图的范例其中每个范例数据点都链接到Tempo中的一个跟踪。
![Using exemplars in Tempo][16]
(Annanay Agarwal, [CC BY-SA 4.0][9])
### 元数据一致性
作为容器化应用程序运行的应用发出的遥测数据通常具有一些相关的元数据。这可以包括集群ID、命名空间、pod IP等。这对于提供基于需求的信息是好的但是如果你可以利用包含在元数据的信息来进行一些高效的工作那就更好了。
 
例如,你可以使用[Grafana云代理将跟踪信息导入Tempo中][17]代理利用Prometheus服务发现机制轮询Kubernetes接口以查询元数据信息并且将这些标记添加到应用程序发出的跨域数据中。由于这些元数据也在Loki中也建立了索引所以通过元数据转换为Loki变迁选择器可以很容易地从跟踪跳转到查看给定服务的日志。
下面是一个一致元数据的示例它可用于Tempo跟踪中查看给定范围的日志。
### ![][18]
### 云本地
Grafana Tempo作为一个容器化的应用时可用的你可以在如Kubernetes、Mesos等任何编排引擎上运行它。根据获取/查询路径上的工作负载各种服务可以水平伸缩。你还可以使用云本地对象存储如谷歌云存储、Amazon S3或者Tempo Azure博客存储。更多的信息请阅读Tempo文档中的[架构部分][19]。
### 试一试Tempo
如果这对你和我们一样有用,可以[克隆Tempo仓库][20]试一试。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/2/tempo-distributed-tracing
作者:[Annanay Agarwal][a]
选题:[lujun9972][b]
译者:[RiaXu](https://github.com/ShuyRoy)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/annanayagarwal
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_space_graphic_cosmic.png?itok=wu493YbB (Computer laptop in space)
[2]: https://grafana.com/oss/tempo/
[3]: http://grafana.com/oss/grafana
[4]: https://prometheus.io/
[5]: https://grafana.com/oss/loki/
[6]: https://peter.bourgon.org/blog/2017/02/21/metrics-tracing-and-logging.html
[7]: https://github.com/jaegertracing/jaeger/tree/master/examples/hotrod
[8]: https://opensource.com/sites/default/files/uploads/tempo_gantt.png (Gantt chart from Grafana Tempo)
[9]: https://creativecommons.org/licenses/by-sa/4.0/
[10]: https://opensource.com/article/19/8/how-set-apache-cassandra-cluster
[11]: https://cortexmetrics.io/
[12]: http://github.com/grafana/tempo
[13]: https://grafana.com/docs/tempo/latest/getting-started/example-demo-app/
[14]: https://grafana.com/blog/2020/10/29/grafana-7.3-released-support-for-the-grafana-tempo-tracing-system-new-color-palettes-live-updates-for-dashboard-viewers-and-more/
[15]: https://grafana.com/blog/2020/11/09/trace-discovery-in-grafana-tempo-using-prometheus-exemplars-loki-2.0-queries-and-more/
[16]: https://opensource.com/sites/default/files/uploads/tempo_exemplar.png (Using exemplars in Tempo)
[17]: https://grafana.com/blog/2020/11/17/tracing-with-the-grafana-cloud-agent-and-grafana-tempo/
[18]: https://lh5.googleusercontent.com/vNqk-ygBOLjKJnCbTbf2P5iyU5Wjv2joR7W-oD7myaP73Mx0KArBI2CTrEDVi04GQHXAXecTUXdkMqKRq8icnXFJ7yWUEpaswB1AOU4wfUuADpRV8pttVtXvTpVVv8_OfnDINgfN
[19]: https://grafana.com/docs/tempo/latest/architecture/architecture/
[20]: https://github.com/grafana/tempo

View File

@ -0,0 +1,166 @@
[#]: subject: (Learn Python dictionary values with Jupyter)
[#]: via: (https://opensource.com/article/21/3/dictionary-values-python)
[#]: author: (Lauren Maffeo https://opensource.com/users/lmaffeo)
[#]: collector: (lujun9972)
[#]: translator: (DCOLIVERSUN)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
用 Jupyter 学习 Python 字典
======
字典数据结构可以帮助你快速访问信息。
![Python书与放在键盘上的手 ][1]
字典是 Python 编程语言使用的数据结构。一个 Python 字典由多个键值对组成;每个对将键映射到其关联的值上。
例如你是一名老师,想把学生姓名与成绩对应起来。你可以使用 Python 字典,将学生姓名映射到他们关联的成绩上。此时,键值对中键是姓名,值是对应的成绩。
如果你想知道某个学生的考试成绩,你可以从字典中访问。这种快捷查询方式可以为你节省解析整个列表找到学生成绩的时间。
本文介绍了如何通过键访问对应的字典值。学习前,请确保你已经安装了[Anaconda 包管理器][2]和 [Jupyter 笔记本][3]。
### 1\. 在 Jupyter 中打开一个新的笔记本
首先打开并在 Web 浏览器中运行 Jupyter。然后
1. 转到左上角的 **文件**
2. 选择 **新笔记本**,点击 **Python 3**
![新建 Jupyter 笔记本][4]
(Lauren Maffeo, [CC BY-SA 4.0][5])
开始时,新建的笔记本是无标题的,你可以将其重命名为任何名称。我为我的笔记本取名为 **OpenSource.com 数据字典教程**
笔记本中标有行号的位置就是你写代码的区域,也是你输入的位置。
在 macOS 上,可以同时按 **Shift**、 **Return**键得到输出。在创建新的代码区域前,请确保完成上述行为;否则,你写的任何附加代码可能无法运行。
### 2\. 新建一个键值对
在字典中输入你希望访问的键与值。输入前,你需要在字典上下文中定义它们的含义:
```
empty_dictionary = {}
grades = {
    "Kelsey": 87,
    "Finley": 92
}
one_line = {a: 1, b: 2}
```
![定义字典键值对的代码][6]
(Lauren Maffeo, [CC BY-SA 4.0][5])
这段代码让字典将特定键与其各自的值关联起来。字典按名称存储数据,从而可以更快地查询。
### 3\. 通过键访问字典值
现在你想查询指定的字典值;在上述例子中,字典值指特定学生的成绩。首先,点击 **Insert** 后选择 **Insert Cell Below**
![在 Jupyter 插入新建单元格][7]
(Lauren Maffeo, [CC BY-SA 4.0][5])
在新单元格中,定义字典中的键与值。
然后,告诉字典打印该值的键,找到需要的值。例如,查询名为 Kelsey 的学生的成绩:
```
# 访问字典中的数据
grades = {
    "Kelsey": 87,
    "Finley": 92
}
print(grades["Kelsey"])
87
```
![查询特定值的代码][8]
(Lauren Maffeo, [CC BY-SA 4.0][5])
当你查询 Kelsey 的成绩(也就是你想要查询的值)时,如果你用的是 macOS只需要同时按 **Shift****Return** 键。
你会在单元格下方看到 Kelsey 的成绩。
### 4\. 更新已有的键
当把一位学生的错误成绩添加到字典时,你会怎么办?可以通过更新字典、存储新值来修正这类错误。
首先,选择你想更新的那个键。在上述例子中,假设你错误地输入了 Finley 的成绩,那么 Finley 就是你需要更新的键。
为了更新 Finley 的成绩,你需要在下方插入新的单元格,然后创建一个新的键值对。同时按 **Shift****Return** 键打印字典全部信息:
```
grades["Finley"] = 90
print(grades)
{'Kelsey': 87; "Finley": 90}
```
![更新键的代码][9]
(Lauren Maffeo, [CC BY-SA 4.0][5])
单元格下方输出带有 Finley 更新成绩的字典。
### 5\. 添加新键
假设你得到一位新学生的考试成绩。你可以用新键值对将那名学生的姓名与成绩补充到字典中。
插入新的单元格,以键值对形式添加新学生的姓名与成绩。当你完成这些后,同时按 **Shift****Return** 键打印字典全部信息:
```
grades["Alex"] = 88
print(grades)
{'Kelsey': 87, 'Finley': 90, 'Alex': 88}
```
![添加新键][10]
(Lauren Maffeo, [CC BY-SA 4.0][5])
所有的键值对输出在单元格下方。
### 使用字典
请记住,键与值可以是任意数据类型,但它们很少是[<ruby>扩展数据类型<rt>non-primitive types</rt>></ruby>>][11]。此外,字典不能以指定的顺序存储、组织里面的数据。如果你想要数据有序,最好使用 Python 列表,而非字典。
如果你考虑使用字典,首先要确认你的数据结构是否是合适的,例如像电话簿的结构。如果不是,列表、元组、树或者其他数据结构可能是更好的选择。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/3/dictionary-values-python
作者:[Lauren Maffeo][a]
选题:[lujun9972][b]
译者:[DCOLIVERSUN](https://github.com/DCOLIVERSUN)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/lmaffeo
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python-programming-code-keyboard.png?itok=fxiSpmnd (Hands on a keyboard with a Python book )
[2]: https://docs.anaconda.com/anaconda/
[3]: https://opensource.com/article/18/3/getting-started-jupyter-notebooks
[4]: https://opensource.com/sites/default/files/uploads/new-jupyter-notebook.png (Create Jupyter notebook)
[5]: https://creativecommons.org/licenses/by-sa/4.0/
[6]: https://opensource.com/sites/default/files/uploads/define-keys-values.png (Code for defining key-value pairs in the dictionary)
[7]: https://opensource.com/sites/default/files/uploads/jupyter_insertcell.png (Inserting a new cell in Jupyter)
[8]: https://opensource.com/sites/default/files/uploads/lookforvalue.png (Code to look for a specific value)
[9]: https://opensource.com/sites/default/files/uploads/jupyter_updatekey.png (Code for updating a key)
[10]: https://opensource.com/sites/default/files/uploads/jupyter_addnewkey.png (Add a new key)
[11]: https://www.datacamp.com/community/tutorials/data-structures-python