python从字典里搜索_Python:在字典中搜索字典的函数
我有個問題是這樣的Write the contract, docstring and implementation for a function findActor that takes a movie title and a character's name and returns the actor/actress that played the given character in the given movie. If the given movie or the given character is not found, it prints out an error message and returns an empty string
我已經完成了以下功能,這些功能將有助于實現這一點。myIMDb是一個全局字典,設置為一個空的dic來啟動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]
myIMDb[len(myIMDb)] = {title: dict2}
return myIMDb
def listMovies():
"""returns a list of titles of all the movies in the global variable myIMDb"""
titles = []
for i in range (len(myIMDb)):
titles.append((list(myIMDb[i].keys())))
return titles
這就是我的問題所在。當我想寫findActor函數時,我沒有得到任何可返回的東西。我還沒有完成這個功能,但我認為我做了一些根本性的錯誤。我覺得自己走錯了路,越寫越迷路。這就是我的目的。如有任何關于如何糾正這艘沉船的建議,我們將不勝感激。在
^{pr2}$
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python从字典里搜索_Python:在字典中搜索字典的函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux mysql timestam
- 下一篇: mysql主从复制思考_Mysql主从复