python元组里嵌套元组
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.
在这里,我们将看到一些示例以了解如何创建元组? , 将列表转换成元组? , 元组的串联 , 嵌套的元组等。
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.
在这里,我们已经看到,如果要创建带有圆括号的元组,则两种结果都不相同。
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')
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 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元组里嵌套元组