python中的元组_Python中的元组

python中的元组

Tuples are a lot like lists, and that's why we can define them in a pretty similar way as we did to define the lists. Simply put, a tuple is a sequence of data.

元组很像列表,这就是为什么我们可以用与定义列表非常相似的方式来定义它们。 简单地说, 元组是数据序列

What makes them different from lists is that tuples are immutable, i.e., the data inside the tuples can't be modified, which is opposite in the case of lists. Other than this, tuples are very similar to lists and that would make it much easier for us to understand as we already know about lists. If you don't, we suggest you to first go through the List tutorial.

它们与列表的不同之处在于,元组是不可变的 ,即,不能修改元组内部的数据,这与列表相反。 除此之外,元组与列表非常相似,这将使我们更容易理解列表,因为我们已经了解列表。 如果您不这样做,建议您先阅读“ 清单”教程

定义元组 (Defining a Tuple)

To define a tuple, we just have to assign a single variable with multiple values separated by commas, and that variable will be known as a Tuple.

要定义一个元组,我们只需分配一个变量,并用逗号分隔多个值,该变量将被称为元组

>>> myTuple = 1, 2, 3, 4

If you try to print it in IDLE,

如果您尝试将其打印在“ IDLE”中,

>>> print (myTuple);

(1, 2, 3, 4)

(1,2,3,4)

You can see in the above example, that myTuple variable is actually a collection of integers 1, 2, 3 and 4. Also, note those circular brackets which appears while printing, around the integers, these will actually help you to distinguish between lists and tuples. Because in case of lists, we have square brackets around the list elements.

您可以在上面的示例中看到myTuple变量实际上是整数1、2、3和4的集合。另外,请注意打印时在整数周围出现的那些圆括号 ,它们实际上将帮助您区分列表和元组。 因为在列表的情况下,列表元素周围有方括号

You can obviously add data of different types in a single tuple,

您显然可以在一个元组中添加不同类型的数据

>>> secondTuple = 1, 2, "python", 4
>>> print (secondTuple);

(1, 2, "python", 4)

(1、2,“ python”,4)

An empty tuple can be created using the tuple() function or by just using an empty bracket ().

可以使用tuple()函数或仅使用一个空的括号()创建一个空的元组

>>> emptyTuple = ();
>>> anotherEmptyTuple = tuple();

The above statements will create tuples with no elements in it. And the compiler would know that emptyTuple and anotherTuple are tuples, with no elements in them.

上面的语句将创建其中没有任何元素的元组。 而且编译器会知道emptyTupleanotherTuple是元组,其中没有元素。

元组索引 (Indexing in Tuples)

Indexing in tuples is also pretty similar to that in lists, the first element has index zero, and it keeps on increasing for the next consecutive elements. Also, backward indexing is also valid in tuples, i.e., the last element can be accessed using the index -1 and the consecutive previous numbers by -2, -3 and so on. Let's take an example,

元组中的索引也与列表中的索引非常相似,第一个元素的索引为零 ,而下一个连续元素的索引则继续增加。 同样, 向后索引在元组中也是有效的,即,可以使用索引-1-2-3等连续的先前数字来访问最后一个元素。 让我们举个例子

>>> example = "apple", "orange", "banana", "berry", "mango"
>>> print (example[0]);

'apple'

'苹果'

In the table below we have marked the tuple elements for both forward and backward indexing:

在下表中,我们标记了正向和反向索引的元组元素:

ValueForward IndexingBackward Indexing
apple0-5
orange1-4
banana2-3
berry3-2
mango4-1
前向索引 向后索引
苹果 0 -5
橙子 1个 -4
香蕉 2 -3
浆果 3 -2
芒果 4 -1

向元组添加元素 (Adding Elements to a Tuple)

As we know, that tuples are immutable, hence the data stored in a tuple cannot be edited, but it's definitely possible to add more data to a tuple. This can be done using the addition operator. Suppose there is a tuple,

众所周知,元组是不可变的,因此无法编辑存储在元组中的数据,但是绝对有可能向元组添加更多数据。 这可以使用加法运算符完成 。 假设有一个元组,

>>> t = (1, 2, 3, 4, 5)

In case you want to add another element, 7 to the tuple, then you can do it as follows:

如果要向该元组添加另一个元素7 ,则可以按如下所示进行操作:

>>> t = t + (7,)

As you can see, we used the addition operator to add(7,) to the tuple t.

如您所见,我们使用加法运算符add(7,)到元组t

>>> print (t);

(1, 2, 3, 4, 5, 7)

(1,2,3,4,5,7)

Hence, we can add any kind of element to a tuple, using the + operator.

因此,我们可以使用+运算符将任何类型的元素添加到元组。

If we try to think, what else can we do with the + operator, we might realize that it can be used to combine two tuples as well. For example:

如果我们尝试去思考,用+运算符还能做什么,我们可能会意识到它也可以用于合并两个元组。 例如:

>>> print ((1, 2, 5, 8) + (2, 9, 4));

(1, 2, 5, 8, 2, 9, 4)

(1,2,5,8,2,9,4)

You can use a tuple(s) to create another tuple.

您可以使用一个元组来创建另一个元组。

删除元组 (Deleting a Tuple)

In order to delete a tuple, the del keyword is used. In order to delete a tuple named myTuple(which we defined earlier), follow the example below:

为了删除元组,使用了del关键字。 为了删除名为myTuple (我们之前定义)的元组,请遵循以下示例:

>>> del (myTuple);

And myTuple will be deleted from the memory.

并且myTuple将从内存中删除。

在元组中切片 (Slicing in Tuples)

Slicing in tuples, works exactly the same like in the case of lists. Let's start with an example:

在元组中切片的工作方式与列表情况完全相同。 让我们从一个例子开始:

>>> t = (1, 2, 3, 4)
>>> print(t[2:4])

(3, 4)

(3,4)

Here, t[2:4] means, slice the tuple starting from index 2 upto the index 4, and take that slice out.

在这里, t[2:4]表示从索引2到索引4对元组进行切片,然后将其取出。

Slicing can be done backwards as well using the negative indexes for traversing the tuple from backward direction.

也可以使用负索引向后切片,以便从后向遍历元组。

基本操作和功能 (Basic Operations and Functions)

The various operations that we can perform on tuples are very similar to lists. In fact, you just saw the + operator with tuples, it works with tuples just like it works with a list. Some other operators for tuples include:

我们可以对元组执行的各种操作与列表非常相似。 实际上,您刚刚看到带元组的+运算符,它与元组一起使用就像列表一样。 元组的其他一些运算符包括:

乘法 (Multiplication)

Multiplying a tuple by any integer, x will simply create another tuple with all the elements from the first tuple being repeated x number of times. For example, t*3 means, elements of tuple t will be repeated 3 times.

将一个元组乘以任何整数,x会简单地创建另一个元组,第一个元组中的所有元素都重复x次。 例如, t*3表示元组t元素将重复3次。

>>> t = (2, 5)
>>> print (t*3);

(2, 5, 2, 5, 2, 5)

(2、5、2、5、2、5)

加成 (Addition)

Using the addition operator, with two or more tuples, adds up all the elements into a new tuple. For example,

使用具有两个或多个元组的加法运算符,将所有元素加到一个新的元组中。 例如,

>>> t = (2, 5, 0) + (1, 3) + (4,)
>>> print (t);

(2, 5, 0, 1, 3, 4)

(2、5、0、1、3、4)

in关键字中 (in keyword)

in keyword, can not only be used with tuples, but also with strings and lists too. It is used to check, if any element is present in the sequence or not. It returns True if the element is found, otherwise False. For example,

in关键字不仅可以用于元组,还可以用于字符串和列表。 它用于检查序列中是否存在任何元素。 如果找到该元素,则返回True ,否则返回False 。 例如,

>>> t = (1, 2, 3, 6, 7, 8)
>>> 2 in t
>>> 5 in t

True False

真假

len()函数 (len() function)

As you might have already guessed, this function is used to get the number of elements inside any tuple.

您可能已经猜到了,此函数用于获取任何元组中的元素数。

>>> t = 1, 2, 3
>>> print (len(t))

3

3

cmp()函数 (cmp() function)

This is used to compare two tuples. It will return either 1, 0 or -1, depending upon whether the two tuples being compared are similar or not.

这用于比较两个元组。 它将返回10-1 ,取决于是否被比较的两个元组是相似的或没有。

The cmp() function takes two tuples as arguments, where both of them are compared. If T1 is the first tuple and T2 is the second tuple, then:

cmp()函数采用两个元组作为参数,将它们都进行比较。 如果T1是第一个元组, T2是第二个元组,则:

  • if T1 > T2, then cmp(T1, T2) returns 1

    如果T1> T2 ,则cmp(T1,T2)返回1

  • if T1 = T2, then cmp(T1, T2) returns 0

    如果T1 = T2 ,则cmp(T1,T2)返回0

  • if T1 > T2, then cmp(T1, T2) returns -1

    如果T1> T2 ,则cmp(T1,T2)返回-1

max()min()函数 (max() and min() function)

To find the maximum value in a tuple, we can use the max() function, while for finding the minimum value, min() function can be used.

要在一个元组中找到最大值,我们可以使用max()函数,而要找到最小值,可以使用min()函数。

>>> t = (1, 4, 2, 7, 3, 9)
>>> print (max(t))
>>> print (min(t))

9 1

9 1

翻译自: https://www.studytonight.com/python/tuples-in-python

python中的元组

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值