python字典包含字典_Python字典教学

本文是一篇关于Python字典的教程,介绍了字典的基本概念、操作和用途,包括创建、删除、访问、循环技术和字典理解。字典是Python中强大的数据结构,由键值对组成,适用于数据解析和存储。文章通过啤酒和啤酒厂数据集的例子,展示了如何使用字典提高代码的可读性和效率。同时,讨论了字典的优缺点,如快速查找、无序性及空间占用。
摘要由CSDN通过智能技术生成

python字典包含字典

Python Dictionary Tutorial

Python offers a variety of data structures to hold our information — the dictionary being one of the most useful. Python dictionaries quick, easy to use, and flexible. As a beginning programmer, you can use this Python tutorial to become familiar with dictionaries and their common uses so that you can start incorporating them immediately into your own code.

Python提供了各种数据结构来保存我们的信息-字典是最有用的字典之一。 Python字典快速,易于使用且灵活。 作为入门程序员,您可以使用此Python教程来熟悉字典及其常见用法,以便立即开始将它们合并到自己的代码中。

When performing data analysis, you’ll often have data that is an unusable or hard-to-use form. Dictionaries can help here, by making it easier to read and change your data.

在执行数据分析时,您经常会得到无法使用或难以使用的数据。 字典可以通过简化读取和更改数据的方式来提供帮助。

For this tutorial, we will use the Craft Beers data sets from Kaggle. There is one data set describing beer characterstics, and another that stores geographical information on brewery companies. For the purposes of this article, our data will be stored in the beers and breweries variables, each as a list of lists. The tables below give a quick look at what the data look like.

在本教程中,我们将使用Kaggle的Craft Beers数据集 。 有一个描述啤酒特性的数据集,另一个存储了啤酒公司的地理信息。 就本文而言,我们的数据将存储在beersbreweries变量中,每个变量均作为列表列表。 下表快速查看了数据的外观。

This table contains the first row from the beers data set.

该表包含beers数据集的第一行。

abv 抗体 ibu 伊布 id ID name 名称 style 样式 brewery_id brewery_id ounces 盎司
0 0 0.05 0.05 1436 1436 Pub Beer 酒吧啤酒 American Pale Lager 美国淡啤酒 408 408 12.0 12.0

This table contains the first row from the breweries data set.

该表包含breweries数据集的第一行。

name 名称 city state
0 0 Northgate Brewing 北门酿造 Minneapolis 明尼阿波利斯 MN MN

必备知识 (Prerequisite knowledge)

This article assumes basic knowledge of Python. To fully understand the article, you should be comfortable working with lists and for loops.

本文假定您具有Python的基本知识。 为了完全理解本文,您应该习惯使用列表和for循环。

We’ll cover:

我们将介绍:

  • Key terms and concepts to dictionaries
    • Dictionary rules
  • Basic dictionary operations
    • creation and deletion
    • access and insertion
    • membership checking
  • Looping techniques
  • Dictionary comprehensions
  • Dictionary advantages and disadvantages
  • 词典的关键术语和概念
    • 字典规则
  • 基本字典操作
    • 创建和删除
    • 访问和插入
    • 会员检查
  • 循环技术
  • 字典理解
  • 词典的优缺点

成为我们的角色 (Getting into our role)

We will assume the role of a reviewer for a beer enthusiast magazine. We want to know ahead of time what each brewery will have before we arrive to review, so that we can gather useful background information. Our data sets hold information on beers and breweries, but the data themselves are not immediately accessible.

我们将担任啤酒爱好者杂志的审稿人。 我们想提前知道每个啤酒厂将要到达的内容,以便我们收集有用的背景信息。 我们的数据集包含有关啤酒和啤酒厂的信息,但数据本身无法立即访问。

The data are currently in the form of a list of lists. To access individual data rows, you must use a numbered index. To get the first data row of breweries, you look at the 2nd item (the column names are first).

数据当前为列表列表的形式。 要访问单个数据行,必须使用数字索引。 要获得breweries的第一个数据行,请查看第二个项目(列名称为第一列)。

breweries[1]
breweries[1]
 

breweries[1] is a list, so you can also index from it as well. Getting the third item in this list would look like:

breweries[1]是一个列表,因此您也可以从中进行索引。 获取此列表中的第三项将类似于:

If you didn’t know that breweries was data on breweries, you’d have a hard time understanding what the indexing is trying to do. Imagine writing this code and looking at it again 6 months in the future. You’re more than likely to forget, so it merits us reformatting the data in a more readable way.

如果您不知道breweriesbreweries数据,那么您将很难理解索引的作用。 想象一下编写此代码,然后在六个月后再次查看它。 您极有可能会忘记,所以它值得我们以一种更具可读性的方式重新格式化数据。

关键术语和概念 (Key terms and concepts)

Dictionaries are made up of key-value pairs. Looking at the key in a Python dictionary is akin to the looking for a particular word in a physical dictionary. The value is the corresponding data that is associated with the key, comparable to the definition associated with the word in the physical dictionary. The key is what we look up, and it’s the value that we’re actually interested in.

字典由键值对组成 。 在Python字典中查找键类似于在物理词典中查找特定单词。 该值是与键相关联的对应数据,可以与与物理词典中的单词相关联的定义进行比较。 关键是我们要查找的内容,这是我们真正感兴趣的价值。

Python Dictionary Tutorial

We say that values are mapped to keys. In the example above, if we look up the word “programmer” in the English dictionary, we’ll see: “a person who writes computer programs.” The word “programmer” is the key mapped to the definition of the word.

我们说值被映射到键。 在上面的示例中,如果我们在英语词典中查找“ programmer”一词,则会看到:“一个编写计算机程序的人。” “程序员”一词是映射到该词定义的键。

键和值的字典规则 (Dictionary rules for keys and values)

Dictionaries are immensely flexible because they allow anything to be stored as a value, from primitive types like strings and floats to more complicated types like objects and even other dictionaries (more on this later).

字典具有极大的灵活性,因为它们允许将任何东西都存储为值,从原始类型(如字符串和浮点数)到更复杂的类型(如对象,甚至其他字典)(稍后将对此进行详细介绍)。

By contrast, there are limitations to what can be used as a key.

相比之下,可以用作密钥的内容存在限制。

A key is required to be an immutable object in Python, meaning that it cannot be alterable. This rule allows strings, integers, and tuples as keys, but excludes lists and dictionaries since they are mutable, or able to be altered. The rationale is simple: if any changes happen to a key without you knowing, you won’t be able to access the value anymore, rendering the dictionary useless. Thus, only immutable objects are allowed to be keys. A key must also be unique within a dictionary.

键必须是Python中的不可变对象,这意味着它不可更改。 此规则允许将字符串,整数和元组作为键,但排除列表和字典,因为它们是可变的或可以更改的。 基本原理很简单:如果在您不知情的情况下对键进行了任何更改,您将无法再访问该值,从而使字典无用。 因此,只允许不可变的对象作为键。 键在字典中也必须是唯一的。

The key-value structuring of a dictionary is what makes it so powerful, and throughout this post we’ll delve into its basic operations, use cases, and their advantages and disadvantages.

字典的键值结构使它变得如此强大,并且在本博文中,我们将深入研究其基本操作,用例及其优缺点。

基本字典操作 (Basic dictionary operations)

创建和删除 (Creation and deletion)

Let’s start with how to create a dictionary. First, we will learn how to make an empty dictionary since you’ll often find that you want to start with an empty one and populate with data as needed.

让我们从如何创建字典开始。 首先,我们将学习如何制作空字典,因为您经常会发现自己想从空字典开始,并根据需要填充数据。

To create an empty dictionary, we can either use the dict() function with no inputs, or a

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值