这是因为random.shuffle
就地洗牌并且不返回任何东西(这就是为什么你会得到None
).
import random
words = ['red', 'adventure', 'cat', 'cat']
random.shuffle(words)
print(words) # Possible Output: ['cat', 'cat', 'red', 'adventure']
Edit:
根据您的编辑,您需要更改的是:
from random import shuffle
words = ['red', 'adventure', 'cat', 'cat']
newwords = words[:] # Copy words
shuffle(newwords) # Shuffle newwords
print(newwords) # Possible Output: ['cat', 'cat', 'red', 'adventure']
or
from random import sample
words = ['red', 'adventure', 'cat', 'cat']
newwords = sample(words, len(words)) # Copy and shuffle
print(newwords) # Possible Output: ['cat', 'cat', 'red', 'adventure']