首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python数据分析(中英对照)·Tuples 元组

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

作者头像
数媒派
发布于 2022-12-01 07:13:56
发布于 2022-12-01 07:13:56
4320
举报
文章被收录于专栏:产品优化产品优化

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.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Node【工具 01】Node Version Manager nvm 安装使用(Node.js版本管理工具)
懵了,一台电脑安装多个JDK版本我们是常遇到的,前端就不知道怎么处理了,幸亏有了nvm。使用它可以简单解决上面的问题:
yuanzhengme
2025/08/10
3030
node版本管理神器nvm安装使用教程(Windows11版本)
当我们在同时在做几个需求不同的项目时,可能我们需要使用不一样的node.js版本,那么我们就需要在不同的node.js版本中进行切换,我们要在电脑上重复卸载安装多个不同版本的node,非常麻烦,这个时候,我们就可以使用nvm来管理我们安装的node,需要使用哪个就指定哪个即可。
霍格沃兹测试开发Muller老师
2023/08/08
2K0
node版本管理神器nvm安装使用教程(Windows11版本)
nvm下载安装使用教程
在前端开发中,会有老版本或新版本的node项目需要开发,如果我们电脑里面只有一个node版本的话,同时开发不同node版本的项目会比较麻烦,这个时候nvm就很好的派上用场了。
程序媛夏天
2024/01/18
1.6K0
nvm下载安装使用教程
NVM-Windows – Windows随意切换node版本 – 开源项目
在Windows上安装Node.js环境并实现版本切换,通常可以使用nvm-windows(Node Version Manager for Windows)。以下是详细步骤:
收心
2024/11/20
1.1K0
安装nvm --- node.js版本管理和切换工具
安装nvm --- node版本管理和切换工具 搞node,要啥nvm啊? Node版本进展迅猛,不同的工程可以支持不同的node版本,如果安装的node版本和工程支持的不同,就需要费时费力的调试了.
子午僧
2020/04/01
3.5K0
node版本管理神器|nvm安装使用教程
检查是否安装成功,打开cmd命令行窗口,输入nvm,安装成功则会出现如下图的内容:
肥晨
2024/02/19
1.4K0
nvm 安装 node,配置 yarn,cnpm,pnpm
首先配置全局安装路径 由于使用nvm管理node,而每个nodejs的版本都自带npm,所以在每次切换node版本的时候,npm版本也会切换,这就可能导致一些已经安装的全局的其他包,造成原来下载过的包不可用
用户11332765
2024/10/28
1.7K1
nvm 安装 node,配置 yarn,cnpm,pnpm
mac和windows上安装nvm管理node版本
NVM 是 node version manager 的缩写,它是一个用来管理电脑上 node 版本的命令行工具,在日常前端开发中是一个跟 node 一样会经常用到的工具,可以很方便的让我们快速切换不同的node版本。
人人都是码农
2024/08/22
5890
mac和windows上安装nvm管理node版本
fnm:Rust开发的高效Node版本管理工具
fnm 是一个基于 Rust 开发的 Node 版本管理工具,它的目标是提供一个快速、简单且可靠的方式来管理 Node.js 的不同版本。同时,它是跨平台的,支持 macOS、Linux、Windows。🚀 Fast and simple Node.js version manager, built in Rust.
Tinywan
2023/09/06
2.2K0
fnm:Rust开发的高效Node版本管理工具
【图文教程】windows系统使用nvm实现多版本node切换
介绍nvm 是 node version manager(node 版本管理工具)的缩写,是一个命令行工具,用于管理和切换到不同版本的 node.js。
凯哥Java
2022/11/30
4.9K0
【图文教程】windows系统使用nvm实现多版本node切换
Windows环境下 NVM 介绍、下载安装及使用详解
Node.js是一种基于Chrome V8引擎的JavaScript运行时,可以让JavaScript在服务器端运行,从而实现了前后端代码共用。但是,不同版本的Node.js可能会有差异,这就需要我们使用版本管理工具来方便地切换版本。而NVM (Node Version Manager)就是一款非常好用的Node.js版本管理工具,它可以轻松地在不同的Node.js版本之间切换。
Yeats_Liao
2023/12/07
5.6K3
使用nvm管理不同版本的node与npm
随着大前端的快速发展,node版本更新很快,我们在工作中,可以会有老版本的node的项目需要维护,也可能有新版本的node的项目需要开发,如果我们只有一个node版本的话将会很麻烦,nvm可以解决我们的难点
777nx
2023/05/02
1.1K0
使用nvm管理不同版本的node与npm
nvm----nodejs版本管理工具!
nvm全英文也叫node.js version management,是一个nodejs的版本管理工具。nvm和n都是node.js版本管理工具,为了解决node.js各种版本存在不兼容现象可以通过它可以安装和切换不同版本的node.js。
科控物联
2024/03/20
9850
nvm----nodejs版本管理工具!
前端:nodejs版本管理工具nvm介绍
大家使用vue框架开发的朋友可能会遇到首次运行公司项目环境的时候,会出现使用npm install命令安装依赖包的时候出现各种各样的问题,其中很重要的一个错误原因就是因为你的nodejs版本和当时搭建环境的版本不一致造成的。今天就来给大家推荐nvm这款nodejs版本管理工具,可以解决你在实际运行vue项目中的一些问题,一起来看看吧!
小明互联网技术分享社区
2023/11/15
1.4K0
前端:nodejs版本管理工具nvm介绍
Windows下完全卸载node.js并安装node.js的多版本管理工具nvm-windows
由于高版本的node.js导致gulp执行build命令失败,我需要在Windows下卸载掉已有的node.js并安装一个多版本管理工具nvm-windows,方便切换不同版本的node.js。
雨临Lewis
2022/01/11
3.3K0
【工具】用nvm管理nodejs版本切换,真香!
事情的起因,公司的一些老项目需要依赖稳定老版本的nodejs,但是自己的一些项目所需要的是更高版本的nodejs,这就会面临频繁切换版本的情况。看到很多同事小伙伴并没有使用nvm进行版本管理,面对切换的苦恼,遂将本狗使用nvm的方法进行分享,彻底告别nodejs版本切换的困扰。
JavaDog程序狗
2024/10/03
1.3K0
【工具】用nvm管理nodejs版本切换,真香!
通过NVM管理Node.js多版本
本文将展示如何在腾讯云服务器(如CVM或Lighthouse实例)中,通过NVM这一工具在不同的项目中切换Node.js版本,并更新至最新的v14版。
溪歪歪
2020/08/30
8.6K2
通过NVM管理Node.js多版本
简单的nvm语法
nvm(Node Version Manager)是一个用于管理Node.js版本的工具。
肥晨
2024/02/19
9350
Windows下安装及使用NVM
我们可能同时在进行2个或者多个项目,而不同的项目所使用的node版本有可能是不一样的,再或者要用最新的node版本进行试验和学习。在这种情况下,对于维护多个版本的node将会是一件非常麻烦的事情,而nvm就是为解决这个问题而产生的,它可以方便的在同一台设备上进行多个node版本之间切换,而这个正是nvm的价值所在,详细信息可以在nvm官网查看。
青年码农
2020/10/13
2.1K0
Windows下安装及使用NVM
谈后端人眼里的 nvm、yarn、pnpm……
虽然我是做后端的,但也时常关注前端,只是最近觉得前端的各种工具名称太眼花缭乱了,nvm、yarn、pnpm、taro……
北桥苏
2024/08/26
2260
谈后端人眼里的 nvm、yarn、pnpm……
推荐阅读
相关推荐
Node【工具 01】Node Version Manager nvm 安装使用(Node.js版本管理工具)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档