我对Python完全陌生,没有编程经验。我有这个(这我不知道,如果它是一个列表或数组):在Python中随机化/洗牌列表/数组?
from random import choice
while True:
s=['The smell of flowers',
'I remember our first house',
'Will you ever forgive me?',
'I\'ve done things I\'m not proud of',
'I turn my head towards the clouds',
'This is the end',
'The sensation of falling',
'Old friends that have said good bye',
'I\'m alone',
'Dreams unrealized',
'We used to be happy',
'Nothing is the same',
'I find someone new',
'I\'m happy',
'I lie',
]
l=choice(range(5,10))
while len(s)>l:
s.remove(choice(s))
print "\nFalling:\n"+'.\n'.join(s)+'.'
raw_input('')
其中随机选择5-10线和打印他们,但他们在同一顺序打印;即“我说谎”将永远处于底部,如果它被选中。我想知道如何将选定的线条洗牌,以便它们以更随机的顺序出现?
编辑: 所以,当我尝试运行此:
import random
s=['The smell of flowers',
'I remember our first house',
'Will you ever forgive me?',
'I\'ve done things I\'m not proud of',
'I turn my head towards the clouds',
'This is the end',
'The sensation of falling',
'Old friends that have said good bye',
'I\'m alone',
'Dreams unrealized',
'We used to be happy',
'Nothing is the same',
'I find someone new',
'I\'m happy',
'I lie',
]
picked=random.sample(s,random.randint(5,10))
print "\nFalling:\n"+'.\n'.join(picked)+'.'
它似乎运行,但不会显示任何信息。我从Amber的回答中正确输入了这个内容吗?我真的不知道我在做什么。
您的代码中随机选择线和消除* *它们。 – 2013-02-16 22:40:05