Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从列表中删除整数(都是用户输入)_Python - Fatal编程技术网

Python 从列表中删除整数(都是用户输入)

Python 从列表中删除整数(都是用户输入),python,Python,我想写一个函数,从列表中删除某个整数。这两个值都是输入的。例如,removeValues([1,2,3],3)将返回列表[1,2]: def removeValues(aList, n): newList = [] if n in aList: aList.remove(n) newList.append(aList) else: return False 我不确定.remove是否正确 list(filter(lamb

我想写一个函数,从列表中删除某个整数。这两个值都是输入的。例如,
removeValues([1,2,3],3)
将返回列表
[1,2]

def removeValues(aList, n):
    newList = []
    if n in aList:
        aList.remove(n)

        newList.append(aList)
    else:
        return False
我不确定
.remove
是否正确

list(filter(lambda x : x != 3, [1,2,3]))
使用过滤器,过滤器将获取函数引用和元素列表。函数被编写为接受一个列表元素,并根据所需的谓词(如
x!=3
。对于每个元素,函数都会检查谓词,只有当条件返回True时,元素才会包含在输出列表中

正如评论中所说,只需在列表中删除即可完成此任务。

使用


从列表中删除所有特定值的函数可以这样编写:

>>> def removeAll(list, value):
...     while value in list:
...         list.remove(value)
...
>>> a = [1,2,3,3,4]
>>> removeAll(a, 3)
>>> print( a )
[1,2,4]

但是已经有一个方法<代码>列表。为此删除…为什么要将列表附加到另一个列表中<代码>[[1,2]]只需将其移除,然后继续<代码>[1,2]“我不确定删除是否是正确的方法”-您测试过吗?有效吗?我测试了列表。删除,但有错误。TypeError:descriptor“remove”需要一个“list”对象,但收到了一个“int”,所以它说它得到了一个int,需要一个list。但我想删除的是一个int…?这行代码在函数中的位置可能重复?函数定义后的第一行?这是函数的完全替代品这正是我要找的。谢谢
>>> def removeAll(list, value):
...     while value in list:
...         list.remove(value)
...
>>> a = [1,2,3,3,4]
>>> removeAll(a, 3)
>>> print( a )
[1,2,4]