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

Python数据分析(中英对照)·Tuples 元组

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

1.2.3: Tuples 元组

元组是不可变的序列,通常用于存储异构数据。
Tuples are immutable sequences typically used to store heterogeneous data.
查看元组的最佳方式是将其作为一个由多个不同部分组成的单个对象。
The best way to view tuples is as a single object that consists of several different parts.
元组在Python编程中有很多用途。
Tuples have many uses in Python programming.
一个特别重要的用例是当您希望从Python函数返回多个对象时。
One especially important use case is when you want to return more than one object from your Python function.
在这种情况下,您通常会将所有这些对象包装在一个元组对象中,然后返回该元组。
In that case, you would typically wrap all of those objects within a single tuple object, and then return that tuple.
现在让我们看一下使用元组可以执行的一些基本操作。
Let’s now take a look at some of the basic operations that we can do using tuples.
我首先要构造一个元组。
I’m first going to construct a tuple.
我将把它称为大写字母T,让我们在元组中输入一些数字。
I’m going to just call it capital T. And let’s just put in a few numbers in my tuple.
比如说1,3,5,7。
Let’s say 1, 3, 5, and 7.
同样,元组是序列的一种类型。
Again, tuples are a type of sequence.
因此,如果我想知道元组中有多少个对象,我可以使用len函数。
So if I wanted to know how many objects I have in my tuple,I can use the len function.
我还可以连接元组。
I can also concatenate tuples.
所以我可以做一些像T+。
So I can do something like T plus.
我需要一个新的元组。
I need a new tuple here.
比如说9号和11号。
Let’s say 9 and 11.
在本例中,Python向我返回一个新的元组,其中两个元组被放在一起。
And in this case, Python returns a new tuple to me where the two tuples have been put together.
因为元组是序列,所以访问元组中不同对象的方式取决于它们的位置。
Because tuples are sequences, the way you access different objects within a tuple is by their position.
因此,如果我想访问元组中的第二个对象,我会键入大写字母T、方括号和1。
So if I wanted to access the second object in my tuple,I would type capital T, square bracket, and 1.
记住,使用位置1将得到元组中的第二个对象,因为Python中的索引从0开始。
And remember, using position 1 is going to give me the second object in the tuple, because indices in Python start at 0.
您需要熟悉的另一个操作是如何打包和解包元组。
Another operation that you need to be familiar with is how to pack and unpack tuples.
假设我有两个数字,两个变量,x和y。
Imagine I have two numbers– two variables, x and y.
让我们快速创建它们。
Let’s just quickly create them.
假设x等于这个。
Let’s say x is equal to this.
y等于这个。
y is equal to this.
想象一下,如果我想构造一个元组对象。
Imagine now if I wanted to construct a tuple object.
我们可以把这两个数字x和y看作坐标。
We could think of these two numbers x and y as coordinates.
所以我可以这样做。
So I could do something like this.
我可以将坐标定义为一个元组,它由两个对象x和y组成。
I could define my coordinate as a tuple, which consists of two objects, x and y.
如果我现在问Python,坐标对象的类型是什么,Python会告诉我这是一个元组。
If I now ask Python, what is the type of my coordinate object,Python will tell me that’s a tuple.
此操作称为打包元组或元组打包。
This operation is called packing a tuple, or tuple packing.
另一个相关操作是如何解包元组。
Another related operation is how you unpack a tuple.
我们的坐标包含两个数字。
Our coordinate contains two numbers.
我们的坐标对象是一个元组。
Our coordinate object is a tuple.
下面是如何解包这个元组。
Here is how you can unpack this tuple.
假设我想把它分成两个数字–
Let’s say I would like to unpack that into two numbers–
比如说c1和c2,可能是坐标1和坐标2的缩写。
say c1 and c2, perhaps short for coordinate 1 and coordinate 2.
我可以把c2和c2写成一个元组。
I can just write c2 and c2 as a tuple.
然后我可以把坐标赋给那个元组。
And then I can assign coordinate into that tuple.
如果我现在看c1和c2的值,我将观察以下内容。
If I now look at the values of c1 and c2, I will observe the following.
c1包含该元组中的第一个对象。
c1 contains the first object in that tuple.
其中c2包含元组的第二个对象。
Where c2 contains the second object of the tuple.
我们还可以在FOR循环中使用元组,这非常方便。
We can also use tuples in FOR loops, which is extremely handy.
假设我创建了多个坐标。
Let’s say I’ve created multiple coordinates.
在本例中,我的对象坐标是一个列表,它由元组组成,其中每个元组由两个数字组成。
So in this case, my object coordinates is a list which consists of tuples where each tuple consists of two numbers.
如果我想在FOR循环中循环这些对象呢?
What if I wanted to loop over these objects in say a FOR loop?
然后我可以做以下事情。
Then I can do the following.
我可以称这些坐标对为x和y。
I can call these coordinate pairs x and y.
让我把它们用括号括起来,用坐标表示。
Let me enclose these in parentheses here, in coordinates.
我可以让Python打印x和y的值。
And I can ask Python to print the value of x and y.
这就是这里发生的事情。
So this is what’s happening here.
坐标是元组列表。
Coordinates is a list of tuples.
在FOR循环中,我要遍历那个容器,那个坐标序列,一次一个。
In my FOR loop I am going over that container, that sequence of coordinates, one at a time.
这里重点关注的关键部分是如何从元组列表中解包元组。
The key part to focus here is how I unpack the tuples from my list of tuples.
所以语法是坐标中的4x逗号y。
So the syntax is 4x comma y in coordinates.
换句话说,我一次一个地解压坐标列表中的元组。
In other words, I’m unpacking the tuples within the coordinates list one at a time.
关于在循环中解包元组还有一件事。
One more thing about unpacking tuples in a loop.
我不一定需要围绕x和y的括号。
I don’t necessarily need the parentheses surrounding x and y.
所以我也可以在坐标中输入x逗号y。
So I can also just type for x comma y in coordinates.
然后我就有了同样的打印功能。
And then I just have the same print function.
这也行得通。
This also works.
然而,有时在元组周围加上括号会使您更清楚地知道您正在处理一个元组对象。
However, sometimes having the extra parentheses around the tuple will make it clearer to you that you are dealing with a tuple object.
理解如何构造和处理包含多个对象的元组相对容易。
It’s relatively easy to understand how to construct and deal with tuples that contain multiple objects.
但是如果元组中只有一个对象呢?
But what if you just have one object within your tuple?
让我们先试试这个。
Let’s experiment with that first.
让我们从一个元组开始,其中有两个对象,比如2和3。
Let’s start with a tuple where we have two objects, say 2 and 3.
我们知道这是我们构造的元组。
We know this is a tuple from the way we constructed.
我们还可以要求Python向我们返回对象的类型,我们现在碰巧称之为c。
We can also ask Python to return to us the type of the object, which we now happen to call c.
如果我想构造一个只包含一个对象的新元组,您可能会猜测我们可以使用以下结构。
If I wanted to construct a new tuple with just one object in it,you might guess that we could just use the following structure.
我们只需要c型等于括号。
We could just type c is equal to parentheses.
我们把那个号码放在里面。
And we put the one number in there.
但是,如果我们现在问Python,这个对象的类型是什么?
However, if we ask Python now, what is the type of this object?
它实际上不是一个元组。
It’s not actually a tuple.
如果我们通过键入类型括号c来检查这个对象的类型,Python告诉我们c实际上是一个整数。
If we check the type of this object by typing type parentheses c,Python is telling us that c is actually an integer.
但这不是我们想要的。
But this is not what we wanted.
我们想要一个只包含一个对象的元组对象。
We wanted to have a tuple object that contains just one object.
这就是语法有点违反直觉的地方。
This is where the syntax is a little bit counterintuitive.
要构造只有一个对象的元组,我们必须使用以下语法。
To construct a tuple with just one object,we have to use the following syntax.
我们先说c等于。
We start by saying c is equal to.
我们把元组放在括号里。
We put our tuple parentheses.
我们把它放在我们的2号。
We put it in our number 2.
我们加上逗号。
And we add the comma.
当我们现在问Python什么类型的对象是c时,我们知道这是一个元组。
When we now ask Python what type of object is c,we know that this is a tuple.
最后,如果需要,还可以省略括号。
Finally, if you want, you can also omit the parentheses.
这也行得通。
This also works.
然而,有时在元组周围加上括号会使您更清楚地知道您正在处理一个元组对象。
However, sometimes having the extra parentheses around the tuple will make it clearer to you that you are dealing with a tuple object.
理解如何构造和处理包含多个对象的元组相对容易。
It’s relatively easy to understand how to construct and deal with tuples that contain multiple objects.
但是如果元组中只有一个对象呢?
But what if you just have one object within your tuple?
让我们先试试这个。
Let’s experiment with that first.
让我们从一个元组开始,其中有两个对象,比如2和3。
Let’s start with a tuple where we have two objects, say 2 and 3.
我们知道这是我们构造的元组。
We know this is a tuple from the way we constructed.
我们还可以要求Python向我们返回对象的类型,我们现在碰巧称之为c。
We can also ask Python to return to us the type of the object, which we now happen to call c.
如果我想构造一个只包含一个对象的新元组,您可能会猜测我们可以使用以下结构。
If I wanted to construct a new tuple with just one object in it,you might guess that we could just use the following structure.
我们只需要c型等于括号。
We could just type c is equal to parentheses.
我们把那个号码放在里面。
And we put the one number in there.
但是,如果我们现在问Python,这个对象的类型是什么?
However, if we ask Python now, what is the type of this object?
它实际上不是一个元组。
It’s not actually a tuple.
如果我们通过键入类型括号c来检查这个对象的类型,Python告诉我们c实际上是一个整数。
If we check the type of this object by typing type parentheses c,Python is telling us that c is actually an integer.
但这不是我们想要的。
But this is not what we wanted.
我们想要一个只包含一个对象的元组对象。
We wanted to have a tuple object that contains just one object.
这就是语法有点违反直觉的地方。
This is where the syntax is a little bit counterintuitive.
要构造只有一个对象的元组,我们必须使用以下语法。
To construct a tuple with just one object,we have to use the following syntax.
我们先说c等于。
We start by saying c is equal to.
我们把元组放在括号里。
We put our tuple parentheses.
我们把它放在我们的2号。
We put it in our number 2.
我们加上逗号。
And we add the comma.
当我们现在问Python什么类型的对象是c时,我们知道这是一个元组。
When we now ask Python what type of object is c,we know that this is a tuple.
最后,如果需要,还可以省略括号。
Finally, if you want, you can also omit the parentheses.
这也行得通。
This also works.
但代码并不十分清楚。
But the code is not quite as clear.
这就是为什么我建议在使用元组时使用括号。
That’s why I recommend using parentheses whenever you’re using a tuple.

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

常见问题FAQ

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

jamin 大神

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

相关推荐

Python数据分析(中英对照)·Expressions and Booleans 表达式和布尔值

Python数据分析(中英对照)·Expressions and Booleans 表达式和布尔值

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

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

Python数据分析(中英对照)·Building and Examining NumPy Arrays 构建和检查 NumPy 数组

Python数据分析(中英对照)·Building and Examining NumPy Arrays 构建和检查 NumPy 数组

Python数据分析(中英对照)·Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍

Python数据分析(中英对照)·Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍

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

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

标签云
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