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

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)

t='Python','Tuple','Lists'
t2=('Python','Tuple','Lists','Includehelp','best')

e_t=()

u_l_t=('includehelp',)

print('empty tuple:',e_t)
print('Unit length tuple:',u_l_t)
print('first tuple:',t)
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

输出量

empty tuple: ()
Unit length tuple: ('includehelp',)
first tuple: ('Python', 'Tuple', 'Lists')
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:

程序:

t  = 'Python','Tuple','Lists'
t2 = ('Python','Tuple','Lists','Includehelp','best')

print('Concatenation of Tuples:\n',t+t2)

Output

输出量

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

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

t=('Python','Tuple','Lists','Includehelp','best')

l=list(t) #now l is list.
t2=tuple(l) #Here list is converted into tuple.

print('tuple is converted into list:',t)
print('list is converted into tuple:',t2)

Output

输出量

tuple is converted into list: ['Python', 'Tuple', 'Lists', 'Includehelp', 'best']
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,

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

t='Python','Tuple','Lists'
t2=('Python','Tuple','Lists','Includehelp','best')

nested_t=(t,t2)

print('Nested tuple:',nested_t)

Output

输出量

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


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

python元组里嵌套元组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值