从元组列表中删除元组可以使用以下方法:
tuple_list
,要删除其中的元组(1, 2)
,可以使用以下代码:tuple_list = [(1, 2), (3, 4), (5, 6)]
new_tuple_list = [t for t in tuple_list if t != (1, 2)]
这将创建一个新的列表new_tuple_list
,其中不包含(1, 2)
元组。
tuple_list
,要删除其中的元组(1, 2)
,可以使用以下代码:tuple_list = [(1, 2), (3, 4), (5, 6)]
new_tuple_list = []
for t in tuple_list:
if t != (1, 2):
new_tuple_list.append(t)
这将创建一个新的列表new_tuple_list
,其中不包含(1, 2)
元组。
filter()
函数:可以使用filter()
函数来过滤掉要删除的元组。例如,假设我们有一个元组列表tuple_list
,要删除其中的元组(1, 2)
,可以使用以下代码:tuple_list = [(1, 2), (3, 4), (5, 6)]
new_tuple_list = list(filter(lambda t: t != (1, 2), tuple_list))
这将创建一个新的列表new_tuple_list
,其中不包含(1, 2)
元组。
无论使用哪种方法,最终都会得到一个不包含要删除元组的新列表。
领取专属 10元无门槛券
手把手带您无忧上云