0

これがこれまでの私のプログラムです。私がする必要があることは に書かれていdocStringます。

#string, list, list --> Dictionary
def addMovie (title, charList, actList):
    """The function addMovie takes a title of the movie, a list of characters,
    and a list of actors. (The order of characters and actors match one
    another). The function addMovie adds a pair to myIMDb. The key is the title
    of the movie while the value is a dictionary that matches characters to
    actors"""

    dict2 = {}
    for i in range (0, len(charList)):
        dict2 [charList[i]] = actList[i]

    myDict = {title, dict2}
    return myDict

ディクショナリmyIMBdは現在空です。しかし、助けが必要なのはループです。ランナーでこれをやろうとすると。

addMovie("Shutter Island", ['Teddy Daniels','Crazy Lady'],['Leodnardo diCaprio', 'old actress'] )

というエラーが表示されます

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
addMovie("Shutter Island", ['Teddy Daniels','Crazy Lady'],['Leodnardo diCaprio', 'old actress'] )
File "C:\Python33\makeDictionary.py", line 10, in addMovie
myDict = {title, dict2}
TypeError: unhashable type: 'dict'

それは、辞書の中に辞書を入れることができないということですか? もしそうなら、どうすればそれを辞書から非辞書に変更できますか。dict 内に dict を持つことができる場合、なぜこれが機能しないのですか。

4

5 に答える 5

0

これはあなたが望むものです:

  def addMovie (title, charList, actList):
    dict2 = {}
    myDict={}
    for i in range (0, len(charList)):
        dict2 [charList[i]] = actList[i]
    myDict[title] = dict2
    return myDict

  #printed for testing
  print addMovie("Shutter Island", ['Teddy Daniels','Crazy Lady'],['Leodnardo diCaprio', 'old actress'] )
于 2013-10-22T21:27:45.110 に答える
0

他の人が指摘したように、あなたは言う,べきときに言った:

そうは言っても、このバージョンも機能しますが、ループは必要ありません。

def addMovie (title, charList, actList):
    return { title : dict(zip(charList, actList)) }
于 2013-10-22T21:43:20.397 に答える