在 Python 中使用索引删除项目的就地函数

Jik*_*ose 7 python list chaining

只是注意到 Python 中没有函数可以通过索引删除列表中的项目,在链接.

例如,我正在寻找这样的东西:

another_list = list_of_items.remove[item-index]

代替

del list_of_items[item_index]

因为,remove(item_in_list)删除后返回列表item_in_list; 我想知道为什么忽略了索引的类似函数。被包括在内似乎非常明显和微不足道,感觉有理由跳过它。

关于为什么这样的功能不可用的任何想法?

- - - 编辑 - - - -

list_of_items.pop(item_at_index)不合适,因为它不返回没有要删除的特定项目的列表,因此不能用于链接。(根据文档:L.pop([index]) -> item -- 删除并返回 index 处的项目

fal*_*tru 2

使用list.pop

>>> a = [1,2,3,4]
>>> a.pop(2)
3
>>> a
[1, 2, 4]
Run Code Online (Sandbox Code Playgroud)

根据文档:

s.pop([i])

与 x = s[i] 相同;德尔 s[i]; 返回x

更新

对于链接,您可以使用以下技巧。(使用包含原始列表的临时序列):

>>> a = [1,2,3,4]
>>> [a.pop(2), a][1] # Remove the 3rd element of a and 'return' a
[1, 2, 4]
>>> a # Notice that a is changed
[1, 2, 4]
Run Code Online (Sandbox Code Playgroud)