且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

函数中的Python修改列表

更新时间:2023-11-28 23:21:28

您必须返回并将返回的值分配给list1:

You have to return and assign the returned value to list1:

def func(list1):
    list1 = list1[2:]    
    print(list1)   # prints [2, 3]
    return list1

list1=func(list1)

,否则您必须在func()内将list1引用为global.

or you have to reference list1 as global inside func().