小编Sha*_*ing的帖子

如何更改Python切片时,如何使Python列表可变

Python的切片操作会创建列表的指定部分的副本.如何传递父列表的切片,以便在此切片更改时,父列表的相应部分随之更改?

def modify(input):
    input[0] = 4
    input[1] = 5
    input[2] = 6


list = [1,2,3,1,2,3]
modify(list[3:6])
print("Woud like to have: [1,2,3,4,5,6]")
print("But I got: "  + str(list))
Run Code Online (Sandbox Code Playgroud)

输出:

想拥有:[1,2,3,4,5,6]
但我得到了:[1,2,3,1,2,3]

python list mutable

1
推荐指数
1
解决办法
572
查看次数

标签 统计

list ×1

mutable ×1

python ×1