Tuples 元组

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.


  转载请注明: 金点帝国 Tuples 元组

 上一篇
Lists 列表 Lists 列表
1.2.2: Lists 列表列表是任何类型的对象的可变序列。Lists are mutable sequences of objects of any type.它们通常用于存储同质项目。And they’re typically use
下一篇 
Ranges 范围 Ranges 范围
1.2.4: Ranges 范围范围是不可变的整数序列,通常用于for循环。Ranges are immutable sequences of integers,and they are commonly used in for loops
  目录