Python|从给定列表中删除尾随的空元素
给定一个列表,任务是从列表的最后一个中删除尾随的None值。让我们讨论一些解决给定任务的方法。
例子:
Input: [1, 2, 3, 4, None, 76, None, None]
Output: [1, 2, 3, 4, None, 76]
Input: [1, 2, None, None, None, None, None, 5]
Output: [1, 2, None, None, None, None, None, 5]
方法#1:使用朴素的方法
# Python code to demonstrate
# to remove trailing None
# elements from lists
# initialising lists
ini_list = [1, 2, 3, 4, None, 76, None, None, None]
# printing initial dictionary
print ("initial dictionary", str(ini_list))
# code toremove trailing None values
# from lists
while not ini_list[-1]:
ini_list.pop()
# printing result
print ("resultant list", str(ini_list))
输出:
initial dictionary [1, 2, 3, 4, None, 76, None, None, None]
resultant list [1, 2, 3, 4, None, 76]
方法 #2:使用itertools.dropwhile()
# Python code to demonstrate
# to remove trailing None
# elements from lists
from itertools import dropwhile
# initialising lists
ini_list = [1, 2, 3, 4, None, 76, None, None, None]
# printing initial dictionary
print ("initial dictionary", str(ini_list))
# code toremove trailing None values
# from lists
res = list(reversed(tuple(dropwhile(lambda x: x is None,
reversed(ini_list)))))
# printing result
print ("resultant list", str(res))
输出:
initial dictionary [1, 2, 3, 4, None, 76, None, None, None]
resultant list [1, 2, 3, 4, None, 76]
方法 #3:使用itertools.takewhile()
# Python code to demonstrate
# to remove trailing None
# elements from lists
from itertools import takewhile
# initialising lists
ini_list = [1, 2, 3, 4, None, 76, None, None, None]
# printing initial dictionary
print ("initial dictionary", str(ini_list))
# code toremove trailing None values
# from lists
res = ini_list[:-len(list(takewhile(lambda x: x == None,
reversed(ini_list))))]
# printing result
print ("resultant list", str(res))
输出:
initial dictionary [1, 2, 3, 4, None, 76, None, None, None]
resultant list [1, 2, 3, 4, None, 76]
方法#4:使用枚举和列表推导
# Python code to demonstrate
# to remove trailing None
# elements from lists
# initialising lists
ini_list = [1, 2, 3, 4, None, 76, None, None, None]
# printing initial dictionary
print ("initial dictionary", str(ini_list))
# code toremove trailing None values
# from lists
res = [x for n, x in enumerate(ini_list)
if any(y is not None for y in ini_list[n:])]
# printing result
print ("resultant list", str(res))
输出:
initial dictionary [1, 2, 3, 4, None, 76, None, None, None]
resultant list [1, 2, 3, 4, None, 76]