金点网络-全网资源,一网打尽
  • 网站首页
    • 金点部落
    • 小游戏
    • OpenAPI
    • 设计资产导航
    • 升级会员
  • 技能学习
    • 体育运动
    • 办公教程
    • 口才演讲
    • 小吃技术
    • 建站教程
    • 摄影教程
    • 棋牌教程
    • 网赚教程
      • 爆粉引流
      • 自媒体
      • 贴吧引流
  • 网站源码
    • 商城/淘客/交易
    • 小说/漫画/阅读
    • 影视/音乐/视频
    • 微信/微商/微擎
    • 理财/金融/货币
    • 模板/主题/插件
  • 游戏源码
    • 精品网单
    • 端游源码
    • 手游源码
    • 页游源码
  • 素材资料
    • 电子文档
    • 综合资料
    • 考研资料
    • 设计素材
    • 音频讲座
      • 人文艺术
      • 名师讲座
      • 说书小说
  • 软件工具
    • Windows软件
    • MacOS软件
    • Android软件
  • 寄售资源
    • 游戏源码
    • 网站源码
    • 软件源码
  • 公益服
登录/注册
  • 专享大神特权
立即开通开通会员抄底价

Python数据分析(中英对照)·Dictionaries 字典

作者 : jamin 本文共7210个字,预计阅读时间需要19分钟 发布时间: 2020-10-18 共1512人阅读

1.2.7: Dictionaries 字典

字典是从键对象到值对象的映射。
Dictionaries are mappings from key objects to value objects.
字典由键:值对组成,其中键必须是不可变的,值可以是任何值。
Dictionaries consists of Key:Value pairs, where the keys must be immutable and the values can be anything.
词典本身是可变的,因此这意味着一旦创建词典,就可以动态修改其内容。
Dictionaries themselves are mutable so this means once you create your dictionary, you can modify its contents on the fly.
字典可用于对无序数据执行非常快速的查找。
Dictionaries can be used for performing very fast look-ups on unordered data.
关于词典,需要注意的一个关键方面是它们不是序列,因此不保持任何类型的左右顺序。
A key aspect to be aware about regarding dictionaries is that they are not sequences, and therefore do not maintain any type of left-right order.
这意味着,如果在字典上循环,Key:Value对将以任意顺序迭代。
This means that if you’re looping over a dictionary,the Key:Value pairs will be iterated over in arbitrary order.
让我们看一个图表来阐明这个观点。
Let’s look at a diagram to clarify this idea.
我们将建立一个简单的字典,其中有与value对象关联的第一个键。
We’re going to set up a simple dictionary where we have our first key that’s associated with a value object.
我们有第二把钥匙,和另一个物体在一起。
We have our second key that goes with another object.
假设我们这里有第四个键,它和相应的值对象一起。
And let’s say we have key number four here which goes with the corresponding value object.
如果这是一个字典,那么这个键对象将始终与这个值对象相关联。
If this is a dictionary, this key object will always be associated with this value object.
类似地,此键将始终与此值对象一起使用。
Similarly, this key will always go with this value object.
当我们说字典不维护任何类型的左或右顺序时,我们所说的是这些键值对本身的顺序没有定义。
When we say that dictionaries don’t maintain any type of left or right order, what we’re saying is that the ordering of these key-value pairs themselves is not defined.
这意味着如果我在这些对上循环,我可能首先得到对应于我的第二个密钥的对。
That means if I’m looping over these pairs,I might first get the pair that corresponds to my second key here.
然后让我们看看字典的一些用法。
Let’s then look at some uses of dictionaries.
我想编一本叫做《年龄》的字典。
I would like to set up a dictionary which is called age.
如果我希望这是一个空字典,我有两种方法来构造它。
And if I want this to be an empty dictionary,I have two ways to construct that.
第一种方法是只使用一对花括号,这会给我一个字典年龄,在构建时它是空的。
The first approach is just to use a pair of curly braces,and this would give me a dictionary age which would be empty at the time of construction.
另一种可能是键入“age=dict”,这是一个关键字,用左括号和右括号括起来。
Another possibility is to type "age = dict",which is a key word, open and close parentheses.
我又一次创建了一本空字典。
And again I have created an empty dictionary.
然后,让我们构建一个包含姓名和年龄的词典。
Let’s then construct a dictionary that consists of names and ages.
在这个特定的字典中,名称将成为键,年龄将成为值对象。
In this particular dictionary, the names are going to be the keys and the ages are going to be the value objects.
想象一下,我们要做的第一项任务是查找一个人的年龄。
Imagine the first task we’d like to do is look up a person’s age.
我们使用以下语法来实现这一点。
We do this using the following syntax.
我们首先键入词典的名称。
We first type the name of the dictionary.
然后用方括号表示。
We then follow with the square brackets.
在方括号内,插入我们感兴趣查找的键。
Inside the square brackets, we insert the key we’re interested in looking up.
比如说,在这种情况下,我们想知道Pam多大了?
Let’s say, in this case, we’d like to know, how old is Pam?
所以我们在方括号内输入“Pam”。
So we’d enter "Pam" inside the square brackets.
Python将结果返回给我们。
And Python returns the result to us.
通常,我们希望修改与特定键关联的值对象。
Often, we’d like to modify the value objects that are associated with specific keys.
假设我们想把Tim的年龄提高一年。
Let’s say we would like to increase the age of Tim by one year.
我们可以这样做。
Here is how we could do this.
我们先拿我们的年龄字典。
We first take our age dictionary.
我们想修改提姆。
We’d like to modify Tim.
假设我们想把他的年龄提高一岁。
And let’s say we want to increase his age by one year.
我们可以使用以下语法。
We could use the following kind of syntax.
我们可以说Tim的年龄等于Tim的年龄加1。
We could say age of Tim is equal to age of Tim plus 1.
这当然有效。
And this certainly works.
然而,将一个对象的值增加一个或其他数字是一种非常常见的操作。
However incrementing the value of an object by one, or by some other number,is a very common operation.
在Python中有一个非常方便的简写符号。
And there’s a very handy shorthand notation for doing that in Python.
让我们以Tim的年龄为例,假设我们想将其增加一年,我们可以做以下事情:我们只需输入“+=1”。
So let’s take the age of Tim,and say we would like to increase that by one year,we can do the following:We can just type "+= 1".
如果我们检查Tim的年龄会发生什么,我们会再次将Tim年龄增加一年。
And what happens if we check the age of Tim,we will have increased Tim age again by one year.
让我们确保我们理解当我们说加号等于时会发生什么。
Let’s make sure we understand what happens when we say plus equals.
当您从左向右读取代码时,加号操作首先发生。
When you’re reading code from left to right,the plus operation happens first.
然后我们有了等号,这意味着赋值。
Then we have the equal sign, which means assignment.
例如,当我们说某物加上等于1时,我们首先获取对象的值,我们向该对象添加一个值,然后将其重新分配回原始对象。
So when we say something plus equals 1, for example, we first take the value of the object, we add one to that object,and then we reassign it back to the original object.
一个常见的错误是有时混淆了这两个操作的顺序。
A common mistake is sometimes to mix up the order of these two operations.
想想如果你说x等于+1会发生什么。
Think about what happens if you say x equals plus 1.
您没有将x的值增加1。
You’re not incrementing the value of x by 1.
相反,你是说x应该被赋值为+1,这和x等于1是一样的。
Instead, you’re saying that x should be assigned the value of plus 1, which is just the same thing as saying x is equal to 1.
如果希望将变量的值递增1,请先递增,然后赋值。
If you wish to increment the value of a variable by 1,you first do it the incrementation and then the assignment.
这就是为什么这两个运算的顺序是加号然后等于。
That’s why the order of these two operations is plus and then equals.
您可以使用dictionary方法键来找出dictionary中的所有键。
You can use the dictionary method keys to find out what are all the keys in the dictionary.
同样,您可以使用values方法找出字典中的所有值。
And similarly, you can use the values method to find out what are all of the values in the dictionary.
当您调用这些方法时,Python将返回一个具有非常特殊类型的对象。
When you call these methods, Python returns to you an object which has a very special type.
返回对象的类型称为“视图对象”。
The type of the returned object is what’s called a "view object".
视图对象所做的正是您所想象的。
View objects do precisely what you might imagine that they do.
它们提供字典中键或值的动态视图。
They provide a dynamic view of the keys or values in the dictionary.
这里的一个关键点是,当您更新或修改字典时,视图也会相应地改变。
A key point here is that as you update or modify your dictionary,the views will also change correspondingly.
现在让我们使用刚才定义的字典。
Let’s now work with the dictionary that we just defined.
我们将首先提取字典中的所有名称。
We’re going to first extract all of the names in the dictionary.
所以我会说“names=age.keys”。
So I’ll do that by saying "names = age.keys".
现在让我们检查一下对象名的类型。
Now let’s just check the type of the object names.
Python确认这是一个dictionary keys对象。
And Python confirms that this is a dictionary keys object.
假设我想在我的字典中添加一个新键。
Let’s say I would like to add a new key to my dictionary.
我们再加上一个叫汤姆的人,他在我们的字典里已经50岁了。
Let’s add a person called Tom who is 50 in our dictionary.
所以我们说年龄[“Tom”],然后将值对象50赋给该键。
So we say age["Tom"] and we assign the value object 50 to that key.
如果我们现在问,名称是什么,您将看到view对象也包含Tom。
If we now ask, what are the names, you’ll see that the view object also contains Tom.
我们没有重新定义名称的内容,这就是视图对象的本质。
We didn’t redefine the content of names,and this is the nature of view object.
当我们修改字典时,视图对象的内容将自动更改。
As we modify the dictionary, the content of the view object will change automatically.
让我们看看values方法。
Let’s look at the values method.
因此,如果我们说age.values,Python将返回字典中的所有值。
So if we say age.values, Python returns to us all of the values in the dictionary.
我将称之为年龄,我们可以看看这个物体。
I’m going to call this ages,and we can look at that object.
所以在这个例子中,我们有一个,两个,三个,四个,五个不同的年龄段在字典里。
So in this case, we have one, two, three, four,five different ages contained in the dictionary.
我打算在字典里再加一个人。
I’m going to add one more person to the dictionary.
我们给那个人打电话叫尼克,尼克31岁了。
Let’s call that person Nick,and let’s say Nick is 31 years old.
如果我现在问Python,名称是什么,您将看到名称Nick也包含在names对象中。
If I now ask Python, what are the names, you’ll see that the name Nick is also contained in the names object.
这就是视图对象的本质——它们的内容在您修改字典时动态更新。
This is the nature of views objects–their content is dynamically updated as you modify your dictionary.
最后,让我们看看如何测试字典中的成员资格。
Finally, let’s see how we’ll test for membership in a dictionary.
我们知道我们的字典里有一个关键的汤姆。
We know that we have a key Tom in our dictionary.
如果我想问汤姆是否是我们字典的一员,如果汤姆是我们字典里的一把钥匙,我可以问“汤姆”的年龄?
If I want to ask if Tom is a member of our dictionary,if Tom is a key in our dictionary, I can ask, "Tom" in age?
答案是“真的”。
And the answer is going to be "True".
如果我问,“佐菲亚”在我们的字典里吗?
If I ask, is "Zofia" in our dictionary?
Python会说“False”。
Python is going to say "False".
这就是我们在字典中测试对象成员身份的方法。
This is how we test for object membership in a dictionary.

Python 数据分析
本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Dictionaries 字典

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用
jamin

jamin 大神

下一篇
网络协议-组播介绍

相关推荐

Python数据分析(中英对照)·Sequences 序列

Python数据分析(中英对照)·Sequences 序列

Python数据分析(中英对照)·Sets 集合

Python数据分析(中英对照)·Sets 集合

Python数据分析(中英对照)·Modules and Methods 模块和方法

Python数据分析(中英对照)·Modules and Methods 模块和方法

Python数据分析(中英对照)·Random Choice 随机选择

Python数据分析(中英对照)·Random Choice 随机选择

Python数据分析(中英对照)·Measuring Time 测量时间

Python数据分析(中英对照)·Measuring Time 测量时间

标签云
Android Atom ExecutorService ForkJoin GM GM后台 GM授权后台 H5 Java Javascript Linux手工服务端 pipbestcom Python ReentrantLock synchronized ThreadLocal volatile Win一键即玩服务端 一键端 传奇 写作 创业 单机 后台 商业端 外网 安卓 安卓苹果双端 工具 手工端 手游 搭建教程 教程 数据分析 文案 游戏源码 端游 经典 网单 职场 自媒体 视频教程 详细搭建教程 运营后台 页游

近期文章

  • 回合手游【逍遥西游之繁华西游】最新整理单机一键既玩镜像服务端_Linux手工端_GM后台_教程
  • 最新整理精品回合制手游【天书奇谈3D混沌完整版】VM一键单机版_linux手工外网端_隐盟视频教程_授权GM后台_双端
  • 典藏修真页游【诸仙列传OL】最新整理Win系服务端_GM工具_详细外网搭建教程
  • MT3换皮MH【浮生若梦尊享挂机修复版】最新整理单机一键即玩镜像端_Linux手工服务端_安卓苹果双端_GM后台_详细搭建教程
  • 大话回合手游【最新引擎之缥缈西游渡劫版】最新整理Linux手工服务端_安卓苹果双端_管理后台_CDK后台_详细搭建教程_视频教程

分类

  • | wordpress插件 |
  • | wordpress模板 |
  • | 其它模板 |
  • | 帝国模板 |
  • | 织梦插件 |
  • | 织梦模板 |
  • A5源码
  • Android软件
  • APP引流
  • E语言
  • H5
  • LUA
  • QQ营销
  • SEO推广
  • Windows软件
  • 体育运动
  • 信息数据
  • 创业专题
  • 办公教程
  • 口才演讲
  • 名师讲座
  • 商城/淘客/交易
  • 小吃技术
  • 小说/漫画/阅读
  • 建站教程
  • 引流脚本
  • 影视/音乐/视频
  • 影视资源
  • 微信/微商/微擎
  • 微信小程序
  • 微信营销
  • 微擎模块
  • 手游源码
  • 技能学习
  • 抖音课程
  • 摄影教程
  • 棋牌教程
  • 模板/主题/插件
  • 游戏源码
  • 爆粉引流
  • 理财/金融/货币
  • 生活老师
  • 电商客
  • 电子文档
  • 电脑教程
  • 社群营销
  • 站长工具
  • 精品网单
  • 系统工具
  • 素材资料
  • 综合资料
  • 编程经验
  • 网站源码
  • 网络安全
  • 网赚教程
  • 网赚源码
  • 考研资料
  • 脚本/AI/智能
  • 自媒体
  • 英语学习
  • 营销软件
  • 设计素材
  • 说书小说
  • 贴吧引流
  • 软件工具
  • 软文营销
  • 逆向软件
  • 音频讲座
  • 页游源码

提供最优质的资源集合

立即加入 友好社区
金点网络-全网资源,一网打尽

新一代全网资源综合门户网(www.pipbest.com-金点网络)专注服务于互联网,提供各类最新最全的免费源码下载(PHP、ASP、JSP、.NET),更提供免费工具,免费源码下载,软件下载,素材下载,赚钱教程下载,交流论坛等网站运营相关的一切内容,为网友搜罗最有价值的网站源码下载与技术教程等服务!

服务目录
  • 金点OpenAPI
  • 金点云
  • 金点支付
友情链接
  • 数媒派
  • 国家电网
快速搜索

本站由Nice强力驱动

声明: 本站部分内容属于原创转载请注明出处 如有侵权行为请严格参照本站【版权声明】与我们联系,我们将在48小时内容进行处理!

本站部分内容属于原创转载请注明出处 如有侵权行为请严格参照本站【版权声明】与我们联系,我们将在48小时内容进行处理!
© 2016-2023 PipBest.Com - 金点网络 & 金点部落. All rights reserved 京ICP备2022005359号-1
  • 关注有礼
  • 签到
  • 客服
    官方QQ群 常见问题 FAQ

    在线客服

    点我联系

    直接说出您的需求!
    切记!带上资源连接与问题!

    工作时间: 9:30-21:30

  • 暗黑
    模式
  • 全屏
  • 投稿
    赚钱
  • 首页

  • 签到

  • 切换

  • 客服

金点网络-全网资源,一网打尽
  • 登录
  • 注册
or
or
忘记密码?
金点网络-全网资源,一网打尽
  • 网站首页 ►
    • 金点部落
    • 小游戏
    • OpenAPI
    • 设计资产导航
    • 升级会员
  • 技能学习 ►
    • 体育运动
    • 办公教程
    • 口才演讲
    • 小吃技术
    • 建站教程
    • 摄影教程
    • 棋牌教程
    • 网赚教程 ►
      • 爆粉引流
      • 自媒体
      • 贴吧引流
  • 网站源码 ►
    • 商城/淘客/交易
    • 小说/漫画/阅读
    • 影视/音乐/视频
    • 微信/微商/微擎
    • 理财/金融/货币
    • 模板/主题/插件
  • 游戏源码 ►
    • 精品网单
    • 端游源码
    • 手游源码
    • 页游源码
  • 素材资料 ►
    • 电子文档
    • 综合资料
    • 考研资料
    • 设计素材
    • 音频讲座 ►
      • 人文艺术
      • 名师讲座
      • 说书小说
  • 软件工具 ►
    • Windows软件
    • MacOS软件
    • Android软件
  • 寄售资源
    ►
    • 游戏源码
    • 网站源码
    • 软件源码
  • 公益服
×
u3** 刚刚下载了 爆款吸金文案训练

    全网资源·一网打尽

  • 金点出品,必属精品!
  • 发布原创内容,获取高额提成!
  • 我们与你共创美好数字生态!
  • 无特殊说明密码默认:pipbest.com