我有这个:
>>> a = [1, 2, 4] >>> print a [1, 2, 4] >>> print a.insert(2, 3) None >>> print a [1, 2, 3, 4] >>> b = a.insert(3, 6) >>> print b None >>> print a [1, 2, 3, 6, 4]
有没有一种方法可以获取更新的列表作为结果,而不是就地更新原始列表?
我得到的最短信息: b = a[:2] + [3] + a[2:]
b = a[:2] + [3] + a[2:]
>>> >>> a = [1, 2, 4] >>> print a [1, 2, 4] >>> b = a[:2] + [3] + a[2:] >>> print a [1, 2, 4] >>> print b [1, 2, 3, 4]