Python:字典中的字典
这是我目前的节目。我需要做的是写在Python:字典中的字典,python,dictionary,Python,Dictionary,这是我目前的节目。我需要做的是写在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 ma
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'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
addMovie(“快门岛”,“泰迪·丹尼尔斯”,“疯女人”,“莱奥德纳多·迪卡普里奥”,“老女演员])
addMovie中第10行的文件“C:\Python33\makeDictionary.py”
myDict={title,dict2}
TypeError:无法损坏的类型:“dict”
这是不是意味着我不能在字典里放一本字典?如果是这样的话,我该如何把它从字典改成非dict。如果我可以在dict里面放一个dict,为什么这不起作用呢 这不是
dict
,而是set
myDict = {title, dict2}
您应该使用冒号创建dict
myDict = {title: dict2}
但此异常也可能与dict
中的dict
有关
TypeError:无法损坏的类型:“dict”
如果您试图将
dict
用作其他dict
的键或set
的项,则会出现此错误这不是dict
,而是set
myDict = {title, dict2}
您应该使用冒号创建dict
myDict = {title: dict2}
但此异常也可能与dict
中的dict
有关
TypeError:无法损坏的类型:“dict”
如果您试图将
dict
用作其他dict
的键或set
的项,则会出现此错误您想要的是:
myDict = {title: dict2}
您提供的实际上是Python的set-literal。您需要类似以下内容:
myDict = {title: dict2}
您提供的实际上是Python的set literal。您正在创建一个
集。用这个代替
myDict = {title: dict2}
python中的dict
是不可散列的,集合中的某些内容的要求是它是可散列的(这就是如何从错误消息中快速判断您做错了什么)。使用逗号和no:
,您编写的是集合
文字符号
另外,dict
键必须是可散列的,但这不是您的问题。您正在创建一个集。用这个代替
myDict = {title: dict2}
python中的dict
是不可散列的,集合中的某些内容的要求是它是可散列的(这就是如何从错误消息中快速判断您做错了什么)。使用逗号和no:
,您编写的是集合
文字符号
另外,dict
键必须是可散列的,但这不是您的问题。这就是您想要的:
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'] )
这就是你想要的:
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'] )
正如其他人指出的,你说了,
,而你本应该说:
话虽如此,此版本也可以工作,但不需要循环:
def addMovie (title, charList, actList):
return { title : dict(zip(charList, actList)) }
正如其他人指出的,你说了,
,而你本应该说:
话虽如此,此版本也可以工作,但不需要循环:
def addMovie (title, charList, actList):
return { title : dict(zip(charList, actList)) }
不,你可以把一个dict作为一个值,你不会得到一个TypeError
。我还没有写过dict不能是其他dict的值,那你在说什么?您声明将dict放入dict将产生TypeError
,但您将显示将第二个dict作为第一个dict的值(因为title
是字符串,是键)。因为dict中只能有键和值,所以我不得不读这篇文章,因为通过将dict作为dict的值,您将得到一个TypeError
。如果您试图将dict用作其他dict的键或集合项,您将得到这个错误。好的,请随意改进我的解释。不,您可以将dict作为一个值,你不会得到一个TypeError
。我没有写过dict不能是其他dict的值,那么你在说什么?您声明将dict放入dict将产生TypeError
,但您将显示将第二个dict作为第一个dict的值(因为title
是字符串,是键)。由于dict中只能有键和值,因此我不得不读这篇文章,因为如果将dict作为dict的值,您将得到一个TypeError
。如果您尝试将dict用作其他dict的键或集合项,您将得到此错误OK,请随时改进我的解释。