我對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