【发布时间】:2017-04-04 16:26:13
【问题描述】:
我看到有几个关于从列表中删除项目的主题,包括使用 remove()、pop() 和 del。但是这些都不是我想要的,因为我想在删除项目时获得一个新列表。例如,我想这样做:
a = [1, 2, 3, 4, 5, 6]
<fill in > # This step somehow removes the third item and get a new list b and let
b = [1, 2, 4, 5, 6]
我该怎么做?
【问题讨论】:
-
那么
a.pop(2)到底有什么问题? -
@barakmanos 我相信它返回元素而不是新的
list对象,因为 OP 想要它。 -
@khelwood OP 想要创建没有第三项的新列表,而不是没有 int
3 -
b = a[:2]+a[3:] -
b=a[:];b.pop(2) ???