例
以下程序使用 remove() 函数从列表中删除大于指定输入值的元素 −
# input list inputList = [45, 150, 20, 90, 15, 55, 12, 75] # Printing... # removing that current element from the list if the condition is true inputList.remove(i) # printing...例
以下程序使用列表推导式从输入列表中删除大于指定输入值的元素 −
# input list inputList = [45, 150, 20, 90, 15, 55, 12, 75] # Printing...less than or equal to the given input value resultList = [k for k in inputList if k <= inputValue] # printing...# Convert the filter object to a list using the list() function resultList = list(filteredObject) # printing