随机洗牌列表变为 None
Random shuffle list becomes None
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
我在我的代码中打错了字,但我想知道在这种情况下为什么 a 会变成 None。引擎盖下发生了什么?
random.shuffle
就地工作,它不会 return 任何东西(因此默认情况下它 returns None
,因为所有 Python 函数调用应该 return 一些值),因此当您执行 - a = random.shuffle(a)
时,a
变为 None。尝试-
random.shuffle(a)
来自 random.shuffle
-
的文档
random.shuffle(x[, random])
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().
(强调我的)
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
我在我的代码中打错了字,但我想知道在这种情况下为什么 a 会变成 None。引擎盖下发生了什么?
random.shuffle
就地工作,它不会 return 任何东西(因此默认情况下它 returns None
,因为所有 Python 函数调用应该 return 一些值),因此当您执行 - a = random.shuffle(a)
时,a
变为 None。尝试-
random.shuffle(a)
来自 random.shuffle
-
random.shuffle(x[, random])
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().
(强调我的)