python 数组清除行
Python | 清除元组 (Python | Clearing tuple)
While working with the tuples, we may need to clear its elements/records. In this tutorial, we are discussing some of the methods to clear a tuple.
在使用元组时,我们可能需要清除其元素/记录。 在本教程中,我们将讨论一些清除元组的方法。
Method 1: Reinitialize the tuple using pair of round brackets ()
方法1:使用一对圆括号()重新初始化元组
A non-empty tuple can be reinitialized using a pair of round brackets () to clear the tuple.
可以使用一对圆括号()重新初始化一个非空的元组,以清除该元组。
# Clearing a tuple in Python | Method 1
# tuple creation
x = ("Shivang", 21, "Indore", 9999867123)
# printing original tuple
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
print()
# clearing tuple by reinitialize it using
# pair of round brackets "()"
x = ()
print("After clearing tuple....")
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
print()
Output
输出量
x: ('Shivang', 21, 'Indore', 9999867123)
len(x): 4
type(x): <class 'tuple'>
After clearing tuple....
x: ()
len(x): 0
type(x): <class 'tuple'>
Method 2: Reinitialize the tuple using tuple()
方法2:使用tuple()重新初始化元组
A non-empty tuple can be reinitialized using tuple() to clear the tuple.
可以使用tuple()重新初始化非空元组,以清除该元组。
# Clearing a tuple in Python | Method 2
# tuple creation
x = ("Shivang", 21, "Indore", 9999867123)
# printing original tuple
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
print()
# clearing tuple by reinitialize it using "tuple()"
x = tuple()
print("After clearing tuple....")
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
print()
Output
输出量
x: ('Shivang', 21, 'Indore', 9999867123)
len(x): 4
type(x): <class 'tuple'>
After clearing tuple....
x: ()
len(x): 0
type(x): <class 'tuple'>
Method 3: Converting Tuple to List, clearing list and then converting it to Tuple again
方法3:将元组转换为列表,清除列表,然后再次将其转换为元组
Since tuple doesn't have any library function to clear its elements. We can 1) convert a tuple to the list using the list() method, 2) clear the list using List.clear() method, and 3) convert empty list to the tuple again using the tuple() function. This method can also be used to clear the tuple.
由于元组没有任何库函数来清除其元素。 我们可以1)使用list()方法将元组转换为列表,2)使用List.clear()方法清除列表,3)使用tuple()函数再次将空列表转换为元组 。 此方法也可以用于清除元组。
# Clearing a tuple in Python | Method 3
# tuple creation
x = ("Shivang", 21, "Indore", 9999867123)
# printing original tuple
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
print()
# clearing tuple by converting Tuple to List,
# clearing list and then converting it
# to Tuple again
temp_list = list(x) # converting to list
temp_list.clear() # clearing list
x = tuple(temp_list) # converting to tuple
print("After clearing tuple....")
print("x: ", x)
print("len(x): ", len(x))
print("type(x): ", type(x))
Output
输出量
x: ('Shivang', 21, 'Indore', 9999867123)
len(x): 4
type(x): <class 'tuple'>
After clearing tuple....
x: ()
len(x): 0
type(x): <class 'tuple'>
翻译自: https://www.includehelp.com/python/clearing-a-tuple.aspx
python 数组清除行