python传递列表作为函数参数

标签 python list function

friend 们,

下面代码的结果是[] 为什么不是 ['0','1','2']?如果我想让函数foo中的psswd等于number,我该怎么办?

number = ['0','1','2']
def foo(psswd):
    psswd = number[:]

if __name__ == '__main__':
    psswd = []
    foo(psswd)
    print psswd

最佳答案

你需要改变而不是重新绑定(bind),用slice-assigning .

psswd[:] = number[:]

关于python传递列表作为函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9765843/

相关文章:

python - 根据从另一个 DataFrame 构建的条件列表选择 Pandas DataFrame 的子集

c# - 如何将 IEnumerable<IEnumerable<T>> 转换为 List<List<T>>

Bash:将带有空变量的 "$@"参数传递给 sudo -i

c++ - 按值传递函数(?)而不是函数指针?

sql-server - SQL Server 检查约束在正确输入时失败

python - 计算 x1 ^ (x2 ^ (x3 ^ (... ^ xn))) 的最后一位(十进制)数字

python - 我如何转置这些数据以获得类似的东西

python 3.3 : LAN speed test which calculates the read & write speeds and then returns the figure to Excel

python - 为什么在将字符串添加到列表时增强赋值的行为不同

python - 用于同时过滤和转换的列表理解中的中间变量