旧lis中新列表的索引目标

2024-04-16 18:45:34 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个列表,看起来像:

x = [1, 0, 0, 1, 1, 1, 0, 0, 0, 0]

然后我有另一个列表,其中的索引需要从列表x中删除:

x_remove = [1, 4, 5]

然后我可以使用numpy命令delete将其从x中删除,并最终得到:

x_final = np.delete(x, x_remove)
>>> x_final = [0, 0, 1, 0, 0, 0, 0]

到目前为止还不错。现在我发现我不想使用整个列表x,而是从索引2开始。所以基本上:

x_new = x[2:]
>>> x_new = [0, 1, 1, 1, 0, 0, 0, 0]

但是,我仍然需要从x_remove列表中删除索引,但是现在,正如您所看到的,索引的位置与以前不同,因此删除了错误的项。同样的事情也会发生,如果我用另一种方法(即先删除索引,然后使用slice从索引2开始)。所以基本上它会/应该是这样的:

x_new_final = [0, 1, 1, 0, 0]  (first use slice, and the remove list)
x_new_final_v2 = [1, 0, 0, 0, 0]  (first use remove list, and then slice)
x_new_final_correct_one = [0, 1, 0, 0, 0, 0]  (as it should be)

那么,有没有什么方法可以让我从不同的索引(通过切片)开始我的列表,并且仍然使用delete命令删除与完整列表相对应的正确索引呢?你知道吗


Tags: and方法命令numpy列表newuse错误
3条回答
x = [1, 0, 0, 1, 1, 1, 0, 0, 0, 0]
x_remove = [1, 4, 5]

for index,value in enumerate(x):
    for remove_index in x_remove:
        if(index == remove_index-1):
            x[index] = ""

final_list = [final_value for final_value in x if(final_value != "")]
print(final_list)

试试这个简单的方法。。。你知道吗

您可以根据切片位置更改xu remove列表。例如:

slice_location = 2
x = [1, 0, 0, 1, 1, 1, 0, 0, 0, 0]
x_remove = [1, 4, 5]

x_new=x[slice_location:]
x_remove = [x-slice_location for x in x_remove if x-slice_location>0]
x_new = np.delete(x, x_remove)

首先,让我们探讨一下简单删除的替代方法(不考虑起始位置的更改问题):

首先用唯一且易于识别的值创建一个x

In [787]: x = list(range(10))
In [788]: x
Out[788]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

一个列表理解方法-也许不是最快的,但相当清楚,没有bug:

In [789]: [v for i,v in enumerate(x) if i not in x_remove]
Out[789]: [0, 2, 3, 6, 7, 8, 9]

你的np.delete方法:

In [790]: np.delete(x, x_remove)
Out[790]: array([0, 2, 3, 6, 7, 8, 9])

它的缺点是将x转换为数组,这不是一项简单的任务(时间方面)。它还生成了一个新数组。我猜是慢了点。你知道吗

就地清除:

In [791]: y=x[:]
In [792]: for i in x_remove:
     ...:     del y[i]
     ...:     
In [793]: y
Out[793]: [0, 2, 3, 4, 6, 8, 9]

哦-错了。我们需要从头开始(最大索引)。这是一个众所周知的Python“食谱”:

In [794]: y=x[:]
In [795]: for i in x_remove[::-1]:
     ...:     del y[i]
     ...:     
     ...:     
In [796]: y
Out[796]: [0, 2, 3, 6, 7, 8, 9]

在幕后np.delete采取了一种蒙蔽的方式:

In [797]: arr = np.array(x)
In [798]: arr
Out[798]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [799]: mask = np.ones(arr.shape, bool)
In [800]: mask[x_remove] = False
In [801]: mask
Out[801]: 
array([ True, False,  True,  True, False, False,  True,  True,  True,
        True])
In [802]: arr[mask]
Out[802]: array([0, 2, 3, 6, 7, 8, 9])

现在来讨论将x_remove应用于x片的问题。x的切片没有切片参数的记录。也就是说,您不能轻易确定y = x[2:]缺少两个值。(好吧,我可以通过比较xy的一些属性来推断,但不能仅从y来推断)。你知道吗

因此,无论如何进行删除,都必须首先调整x_remove的值。你知道吗

In [803]: x2 = np.array(x_remove)-2
In [804]: x2
Out[804]: array([-1,  2,  3])
In [805]: [v for i,v in enumerate(x[2:]) if i not in x2]
Out[805]: [2, 3, 6, 7, 8, 9]

这是可行的,但是-1可能是一个问题。我们不希望它意味着the last element。所以为了安全,我们必须先过滤掉阴性指标。你知道吗

In [806]: np.delete(x[2:], x2)
/usr/local/bin/ipython3:1: FutureWarning: in the future negative indices will not be ignored by `numpy.delete`.
  #!/usr/bin/python3
Out[806]: array([2, 3, 6, 7, 8, 9])

如果delete不忽略负指数,它可能会得到这样一个掩码-末尾有一个False

In [808]: mask = np.ones(arr[2:].shape, bool)
In [809]: mask[x2] = False
In [810]: mask
Out[810]: array([ True,  True, False, False,  True,  True,  True, False])

相关问题 更多 >