Python:从数字列表中删除负数
问题是去除数字中的否定词 执行Python:从数字列表中删除负数,python,list,negative-number,Python,List,Negative Number,问题是去除数字中的否定词 执行remove_negs([1,2,3,-3,6,-1,-3,1])时,结果是:[1,2,3,6,-3,1]。假设结果是[1,2,3,6,3,1]。发生的情况是,如果一行中有两个负数(例如,-1,-3),则第二个数字将不会被删除。 def main(): 数字=输入(“输入数字列表:”) 删除_negs(数字) def remove_negs(num_列表): ''从列表编号列表中删除负数'' 对于num_列表中的项目: 如果项目>[i代表x中的i如果0要去掉数字的负
remove_negs([1,2,3,-3,6,-1,-3,1])
时,结果是:[1,2,3,6,-3,1]
。假设结果是[1,2,3,6,3,1]
。发生的情况是,如果一行中有两个负数(例如,-1,-3
),则第二个数字将不会被删除。
def main():
数字=输入(“输入数字列表:”)
删除_negs(数字)
def remove_negs(num_列表):
''从列表编号列表中删除负数''
对于num_列表中的项目:
如果项目<0:
数量列表。删除(项目)
打印数字列表
main()
简单得多:
>>> a = [ 1, 2, 3, -3, 6, -1, -3, 1]
>>> [x for x in a if x >= 0 ]
[1, 2, 3, 6, 1]
如果确实要循环,请尝试以下方法:
def remove_negs(num_list):
r = num_list[:]
for item in num_list:
if item < 0:
r.remove(item)
print r
关键是赋值语句r=num\u list[:]
复制num\u list。为了不混淆循环,我们将从r
中删除项,而不是从循环的列表中删除项
更多:Python对变量的处理有点微妙。Python将变量名(如r
或num\u list
与变量数据(如[1,2,3,6,1]
)分开。这些名称只是指向数据的指针。考虑赋值语句:
r = num_list
运行此语句后,r
和num\u list
都指向相同的数据。如果您更改了r
的数据,那么您也更改了num\u list
的数据,因为它们都指向相同的数据。现在,考虑一下:
r = num_list[:]
此语句告诉python通过只获取数据的某些元素来修改num_list
。因此,python复制了num\u list
的数据。恰好,[:]
指定我们希望所有num_list
的数据保持不变,但这并不能阻止python复制。副本被分配到r
。这意味着r
和mum\u list
现在指向不同的数据。我们可以对r
的数据进行更改,但不会影响num\u list
的数据,因为它们的数据不同
如果这对您来说是新的,您可能想看看关于python处理变量名和变量数据的方法的本教程:
示例:
>>> a = [ 1, 2, 3, -3, 6, -1, -3, 1]
>>> b = a # a and b now point to the same place
>>> b.remove(-1)
>>> a
[1, 2, 3, -3, 6, -3, 1]
与之相比:
>>> a = [ 1, 2, 3, -3, 6, -1, -3, 1]
>>> b = a[:] # a and b now point to different data
>>> b
[1, 2, 3, -3, 6, -1, -3, 1]
>>> b.remove(-1)
>>> b
[1, 2, 3, -3, 6, -3, 1]
>>> a
[1, 2, 3, -3, 6, -1, -3, 1]
在遍历列表时从列表中删除元素通常是一个坏主意(请参阅我的评论中的解释,以了解为什么会这样)。更好的方法是使用: 请注意,上面的一行创建了一个新列表,并将
num\u list
分配给该列表。您还可以对表单进行“就地”赋值
num_list[:] = ...
它不会在内存中创建新列表,而是修改
num\u list
已经指向的内存位置。这一差异得到了更详细的解释。来自对阿尔沙吉答案的评论:
但这就消除了负数。我需要删除负面符号,但仍保留列表中的数字
删除负数正是您的代码所要做的,也是获得所需结果的唯一方法:
假设结果为[1,2,3,6,3,1]
但是如果你真的想从数字中“去掉负面符号”,那就更容易了。例如,要从-3
中删除负号,您只需将其取反并得到3
,对吗?您可以在现有代码中执行此操作:
for index, item in enumerate(num_list):
if item < 0:
num_list[index] = -item
当然,无论哪种方式,这都会给您提供
[1,2,3,3,6,1,3,1]
,这是错误的答案……但如果您的评论是正确的,这就是您要求的答案。另一种是传统的变量运算符非变量
也尝试一些=)
>>[i代表x中的i如果0要去掉数字的负号,这是最简单的方法
def remove_negs(somelist):
for each in somelist:
if each < 0:
somelist[somelist.index(each)] = -each
print(somelist)
def remove_negs(somelist):
对于somelist中的每个:
如果每个<0:
somelist[somelist.index(每个)]=-each
打印(somelist)
比如说
rNegatives([-2,5,11,-1])
打印出来
[2,5,11,1]
另一个解决方案
filter( lambda x: x>0, [ 1, 2, 3, -3, 6, -1, -3, 1])
[1, 2, 3, 6, 1]
我认为一个优雅的解决方案可以是这样的:
import numpy as np
x = [1, 2, 3, -3, 6, -1, -3, 1] # raw data
x = np.array(x) # convert the python list to a numpy array, to enable
matrix operations
x = x[x >=0] # this section `[x >=0]` produces vector of True and False
values, of the same length as the list x
# as such, this statement `x[x >=0]` produces only the
positive values and the zeros
print(x)
[1 2 3 6 1] # the result
对于检查val>=0的列表理解来说,这听起来是一个不错的工作。不要从正在迭代的列表中删除项目,因为这会更改项目的索引,最终可能会跳过某些元素。正如其他人所说,在迭代数据容器时,不要更改其内容。在列表(num_list):另外,如果您已经知道某个项目的位置,则不应调用remove
来删除该项目。这可能是错误的,(a)因为remove
会删除与item
相等的第一个项目,而该项目可能不是您想要的,以及(b)缓慢,因为这意味着您必须搜索整个列表才能找到项目,即使您已经知道它在哪里。del num\u list[index]
总是更好。但是您如何知道索引呢?很简单:对于索引,枚举中的项目(num\u list):
。(这只是一个旁注;它不会解决你的实际问题……但是已经有了很好的答案和评论。)我认为OP实际上不需要在这里改变num_list
,所以原始版本(没有切片分配)可能还可以。也许两者都显示并解释差异会更好?@abarnert是的,也许你是对的。我会编辑来解释差异。@user3161743:你说“结果应该是[1,2,3,6,3,1]
”这正是arshajii的代码产生的结果。如果你按照你声称想要的方式去做,结果将是[1,2,3,3,6,1,3,1]
。
num_list = [-item if item < 0 else item for item in num_list]
num_list = [abs(item) for item in num_list]
>>> [i for i in x if 0 <= i]
[1, 2, 3, 6, 1]
def remove_negs(somelist):
for each in somelist:
if each < 0:
somelist[somelist.index(each)] = -each
print(somelist)
filter( lambda x: x>0, [ 1, 2, 3, -3, 6, -1, -3, 1])
[1, 2, 3, 6, 1]
import numpy as np
x = [1, 2, 3, -3, 6, -1, -3, 1] # raw data
x = np.array(x) # convert the python list to a numpy array, to enable
matrix operations
x = x[x >=0] # this section `[x >=0]` produces vector of True and False
values, of the same length as the list x
# as such, this statement `x[x >=0]` produces only the
positive values and the zeros
print(x)
[1 2 3 6 1] # the result