回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>假设我有一个列表,看起来像:</p>
<pre><code>x = [1, 0, 0, 1, 1, 1, 0, 0, 0, 0]
</code></pre>
<p>然后我有另一个列表,其中的索引需要从列表<code>x</code>中删除:</p>
<pre><code>x_remove = [1, 4, 5]
</code></pre>
<p>然后我可以使用<code>numpy</code>命令<code>delete</code>将其从<code>x</code>中删除,并最终得到:</p>
<pre><code>x_final = np.delete(x, x_remove)
>>> x_final = [0, 0, 1, 0, 0, 0, 0]
</code></pre>
<p>到目前为止还不错。现在我发现我不想使用整个列表<code>x</code>,而是从索引2开始。所以基本上:</p>
<pre><code>x_new = x[2:]
>>> x_new = [0, 1, 1, 1, 0, 0, 0, 0]
</code></pre>
<p>但是,我仍然需要从<code>x_remove</code>列表中删除索引,但是现在,正如您所看到的,索引的位置与以前不同,因此删除了错误的项。同样的事情也会发生,如果我用另一种方法(即先删除索引,然后使用slice从索引2开始)。所以基本上它会/应该是这样的:</p>
<pre><code>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)
</code></pre>
<p>那么,有没有什么方法可以让我从不同的索引(通过切片)开始我的列表,并且仍然使用<code>delete</code>命令删除与完整列表相对应的正确索引呢?你知道吗</p>