import random
a = [1,2,3,4,5,6,7,8,9]
# random.shuffle(a) # this is fine
a = random.shuffle(a) # a is now None
Run Code Online (Sandbox Code Playgroud)
我在我的代码中输入了一个拼写错误,但在这种情况下我想知道为什么变成None.引擎盖下发生了什么?
random.shuffle就地工作,它不返回任何东西(因此默认它返回None,因为所有Python函数调用应该返回一些值),因此当你这样做 - a = random.shuffle(a),a变为无.试试 -
random.shuffle(a)
Run Code Online (Sandbox Code Playgroud)
从文件random.shuffle-
random.shuffle(x[, random])将序列x随机移动到位.可选参数random是一个0参数函数,在[0.0,1.0)中返回随机浮点数; 默认情况下,这是函数random().
(强调我的)
| 归档时间: |
|
| 查看次数: |
3154 次 |
| 最近记录: |