python元组里嵌套元组_Python中的元组

╰+攻爆jí腚メ 2023-03-05 07:28 108阅读 0赞

python元组里嵌套元组

Python元组 (Python Tuples)

Tuples in Python are a collection of elements in a round bracket() or not but separated by commas. Tuples are similar to list in some operations like indexing, concatenation, etc. but lists are mutable whereas tuples are immutable acts like a string. In Python, Tuples are converted into lists by using the keyword list and list are converted into tuples by using keyword “tuple”.

Python中的元组是圆括号()中元素的集合,不是,而是用逗号分隔。 元组在某些操作(例如索引,连接等)中类似于列表,但是列表是可变的,而元组是不可变的行为,例如字符串。 在Python中,使用关键字list将元组转换为列表,并使用关键字“ tuple”将 list转换为元

Here we will see some examples to understand how to create tuples?, convert lists into tuples?, concatenation of tuples, nested tuples, etc.

在这里,我们将看到一些示例以了解如何创建元组?将列表转换成元组?元组的串联嵌套的元组等。

创建元组 (Creating Tuples)

  1. t='Python','Tuple','Lists'
  2. t2=('Python','Tuple','Lists','Includehelp','best')
  3. e_t=()
  4. u_l_t=('includehelp',)
  5. print('empty tuple:',e_t)
  6. print('Unit length tuple:',u_l_t)
  7. print('first tuple:',t)
  8. print('Second tuple:',t2)

Note: When you are going to create a tuple of length 1 in Python then you must put a comma after the element before closing the round brackets.

注意:如果要在Python中创建长度为1的元组,则必须在元素后加上逗号,然后再关闭圆括号。

Output

输出量

  1. empty tuple: ()
  2. Unit length tuple: ('includehelp',)
  3. first tuple: ('Python', 'Tuple', 'Lists')
  4. Second tuple: ('Python', 'Tuple', 'Lists', 'Includehelp', 'best')

Here, we have seen that if we are creating tuples with round brackets or not both results are the same.

在这里,我们已经看到,如果要创建带有圆括号的元组,则两种结果都不相同。

元组的串联 (Concatenation of Tuples)

We can simply add tuples like lists by using plus (+) sign between it. Let’s suppose the same tuples that we have created just above and try to add these tuples.

我们可以通过在它们之间使用加号( + )来简单地添加类似列表的元组。 假设我们刚才创建的相同的元组并尝试添加这些元组。

Program:

程序:

  1. t = 'Python','Tuple','Lists'
  2. t2 = ('Python','Tuple','Lists','Includehelp','best')
  3. print('Concatenation of Tuples:\n',t+t2)

Output

输出量

  1. Concatenation of Tuples:
  2. ('Python', 'Tuple', 'Lists', 'Python', 'Tuple', 'Lists', 'Includehelp', 'best')

将列表转换为元组,并将元组转换为列表 (Convert list into tuples and tuples into list)

  1. t=('Python','Tuple','Lists','Includehelp','best')
  2. l=list(t) #now l is list.
  3. t2=tuple(l) #Here list is converted into tuple.
  4. print('tuple is converted into list:',t)
  5. print('list is converted into tuple:',t2)

Output

输出量

  1. tuple is converted into list: ['Python', 'Tuple', 'Lists', 'Includehelp', 'best']
  2. list is converted into tuple: ('Python','Tuple','Lists','Includehelp','best')

嵌套元组 (Nested tuples)

Nested tuples means one tuple inside another like nested lists. To create a nested tuple, we have to simply put both or more tuples in the round brackets separated with commas. Again we will assume the same tuple that we have created above and try to make it in the form of a nested tuple. Let’s look at the program,

嵌套元组是指另一个嵌套内部的元组,如嵌套列表 。 要创建嵌套元组 ,我们只需将两个或多个元组放在用逗号分隔的圆括号中即可。 同样,我们将假定与上面创建的相同的元组,并尝试以嵌套元组的形式进行创建。 让我们看一下程序,

  1. t='Python','Tuple','Lists'
  2. t2=('Python','Tuple','Lists','Includehelp','best')
  3. nested_t=(t,t2)
  4. print('Nested tuple:',nested_t)

Output

输出量

  1. Nested tuple: (('Python', 'Tuple', 'Lists'), ('Python', 'Tuple', 'Lists', 'Includehelp', 'best'))

翻译自: https://www.includehelp.com/python/tuples.aspx

python元组里嵌套元组

发表评论

表情:
评论列表 (有 0 条评论,108人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python

    Python 元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改。 元组使用小括号,列表使用方括号。 元组创建很简单,只需要在括号中添加元素,并使用

    相关 Python

    Python 元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改。 元组使用小括号,列表使用方括号。 元组创建很简单,只需要在括号中添加元素,并使用

    相关 python--

    元组(tuple)的定义 Python的元组与列表类似,不同之处在于元组的元素不能修改 元组使用小括号,列表使用方括号 元组创建很简单,只需要在括号中添加元

    相关 Python

    一、什么是元组 元组是Python中常用的一种数据结构。元组由不同的元素组成,每个元素可以存储不同类型的数据,如字符串、数字、甚至元组。元组是“写保护”的,即元组创建后不能再

    相关 python

    前面我们都是在学习列表的使用,列表这种数据结构适合存储在程序运行期间动态改变的数据集。而有时候偏偏我们不想我们定义的数据被修改,在python中能够达到我们目的的一种数据结构就

    相关 python

    基本操作 与列表相似,元组`Tuple`也是个有序序列,但是元组是不可变的,用`()`生成,可以索引,切片,但是元组是不可变的 ![70][] 单个元素的元组生成