python 数组清除行_在Python中清除元组

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 数组清除行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值