不可散列的类型和替换输入

Unhashable types & replacing inputs

The goal is simple: show a dialog with options, then when a option is selected it will automatically replace some values (to decrease the possibility of making an error while typing the values manually).以下代码是较大代码的一部分,但这是最重要的部分。较大的一段代码是别人写的。我写了以下块:

if expInfo2['quadrant'] == 'UL':
        expInfo['refOrientation':'45','x':'-4.24','y':'4.24']
    elif expInfo2['quadrant'] == 'LL':
        expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']
    elif expInfo2['quadrant'] == 'UR':
        expInfo['refOrientation':'-45','x':'4.24','y':'4.24']
    elif expInfo2['quadrant'] == 'LR':
        expInfo['refOrientation':'45','x':'4.24','y':'-4.24']

代码在读取第二行之前工作正常,并给出以下错误:

Traceback (most recent call last): File "D:\User\File\Experiment.py", line 48, in <module> expInfo['refOrientation':'45','x':'-4.24','y':'4.24'] TypeError: unhashable type ()

我的编程经验有限,但我知道我放在第二行的东西不适合放在一起。但是,我正在考虑将它们一块一块地拆分,但我认为这行不通,如下所示:

if expInfo2['quadrant'] == 'UL':
    expInfo['refOrientation':'45']
    expInfo['x':'-4.24']
    expInfo['y':'4.24']
et cetera...

完整代码:

#present a dialogue to chose lab room and whether eyetracker is on or not
expInfo2 = {'lab':'2','eyetracker': '0','quadrant':''}
dlg = gui.Dlg(title="Info", pos=(200, 400))
dlg.addField('Which lab are you in? 2 for lab-2, 3 for lab-3',expInfo2['eyelab'])
dlg.addField('Do you want the eyetracker on? 0 for yes, 1 for no',expInfo2['eyetracker'])
dlg.addField('What quadrant is used? UL=Upper Left, LL=Lower Left, UR=Upper Right, LR=Lower Right',expInfo2['quadrant'])
inf = dlg.show()

expInfo2['lab']=inf[0]
expInfo2['eyetracker'] = inf[1]
expInfo2['quadrant'] = inf[2]

############################## THIS IS THE CODE FOR LAB 2 ###########################################
if expInfo2['lab'] == '2':


    expInfo = {'observer':'insert','typeofstaircase':'insert','refOrientation':'','startorient':'insert','x':'','y':'','numstair':4,}
    dateStr = time.strftime("%b_%d_%H%M", time.localtime())#add the current time

    if expInfo2['quadrant'] == 'UL':
        expInfo['refOrientation':'45','x':'-4.24','y':'4.24']
    elif expInfo2['quadrant'] == 'LL':
        expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']
    elif expInfo2['quadrant'] == 'UR':
        expInfo['refOrientation':'-45','x':'4.24','y':'4.24']
    elif expInfo2['quadrant'] == 'LR':
        expInfo['refOrientation':'45','x':'4.24','y':'-4.24']

    #present a dialogue to change params
    dlg = gui.Dlg(title="Info", pos=(200, 400))
    dlg.addField('Observer:',expInfo['observer'])
    dlg.addField('Type of staircase?', expInfo['typeofstaircase'])
    dlg.addField('Start Orientation Increment:',expInfo['startorient'])
    dlg.addField('X:',expInfo['x'])
    dlg.addField('Y:',expInfo['y'])
    dlg.addField('Ref. Orienation:',expInfo['refOrientation'])
    #dlg.addField('Number of Staircases',expInfo['numstair'])
    inf = dlg.show()

    expInfo['observer']=inf[0]
    expInfo['typeofstaircase'] = inf[1]
    expInfo['startorient']=inf[2]
    expInfo['x']=inf[3]
    expInfo['y']=inf[4]
    expInfo['refOrientation']=inf[5]
    #expInfo['numstair'] = inf[6]
    #dlg = gui.DlgFromDict(expInfo, title='info', fixed=['date'])
    #if dlg.OK:
    #    print(expInfo)
    #else:
    #    core.quit()#the user hit cancel so exit

有什么建议吗?

expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']

所以这被解释为

expInfo.__getitem__((
   slice('refOrientation', '-45', None),
   slice('x', '-4.24', None),
   slice('y', '-4.24', None)
))

,切片对象的三元组。 expInfo 是一个字典,只接受可哈希类型和

hash((
   slice('refOrientation', '-45', None),
   slice('x', '-4.24', None),
   slice('y', '-4.24', None)
))

引发错误

 TypeError: unhashable type: 'slice'

因为,嗯,他们不是 hashable。在这样的切片中使用字符串非常非常奇怪,所以我认为您没有以正确的方式输入密钥。


我想你想做的是 update 字典。为此,您需要:

if expInfo2['quadrant'] == 'UL':
    expInfo.update({'refOrientation': '45', 'x': '-4.24', 'y': '4.24'})
elif expInfo2['quadrant'] == 'LL':
    ...

(请注意我放入的空格以使其更具可读性。我建议在进一步使用 python 之前先阅读 PEP8,因为您将样式指南从 window).