我想在Python(特別是Psychopy)中使用random.sample函數從函數列表中隨機抽樣。這裏是我的代碼:從Python中的列表中隨機採樣函數的語法
from psychopy import visual, core, event, gui
import random
import string
win = visual.Window([800,600], monitor="testMonitor", units = "pix", color="white",fullscr=False)
def test1():
welcome = "blah1"
welcometext = visual.TextStim(win, text=welcome, wrapWidth=500, color="black")
welcometext.draw()
win.flip()
event.waitKeys()
def test2():
welcome = "blah2"
welcometext = visual.TextStim(win, text=welcome, wrapWidth=500, color="black")
welcometext.draw()
win.flip()
event.waitKeys()
def test3():
welcome = "blah3"
welcometext = visual.TextStim(win, text=welcome, wrapWidth=500, color="black")
welcometext.draw()
win.flip()
event.waitKeys()
mylist = [test1, test2, test3]
random_sample = random.sample(mylist,1)()
當我嘗試運行此代碼,我得到:
TypeError: 'list' object is not callable
我有點失落,爲什麼random.sample並不在這裏工作,所以任何幫助將非常感激。此外,作爲一個側面說明,如果我改變的代碼使用random.choice的最後一位,那麼這個功能似乎完全正常工作:
mylist = [test1, test2, test3]
random_choice = random.choice(mylist)()
'random.sample()'返回一個列表。 – stranac