python - def行在python中做什么

标签 python

<分区>

倒数第二行在这里做什么?它似乎没有分配变量、打印或任何东西,但我尝试以各种方式破解代码,行 doublelist(somelist) 似乎是必要的,但我不知道为什么.

def doubleList(list):
    i=0
    while i<len(list):
        list[i]=list[i]*2
        i=i+1

someList=[34,72,96]
doubleList(someList)
print someList

最佳答案

函数可以修改传递给它们的可变参数。名为“list”的(命名不当的)列表具有(使用非惯用风格)其每个元素乘以 2,就地。例如:

>>> def inplace(seq):
...     seq[0] = 5
... 
>>> a = [1,2,3]
>>> print a
[1, 2, 3]
>>> inplace(a)
>>> a
[5, 2, 3]

关于python - def行在python中做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252207/

相关文章:

python - 从 Python 中的集合中随机选择元素

python - 如何使用 Pandas 创建 days_in_year 函数?

python - Python中(专家系统)的后向和前向链接算法

python - 由于 unicode 解码错误,无法在 pandas 中打开 csv 文件

python - Python 网络抓取、DataFrame 索引问题

python - 在 django 模型中存储云文件链接列表的最佳方式是什么

python - Tkinter 用 {} 显示文本

python - 第二个最新文件

从模块导入全局时,python 包会自动导入

python - concurrent.futures 和 POSIX Linux 中异步 I/O 的区别