我寫了一個簡單的腳本,它的範圍是:如何獲取其值至少包含一個項目在另一個列表中的字典鍵?
list=[1,19,46,28 etc...]
dictionary={Joey:(10,2,6,19), Emily: (0,3), etc}
現在我需要找到具有在值
例如至少列表中的一個條目的字典中的所有鍵: 19是喬斯的價值觀,所以喬伊是贏家。
我怎麼做的:(無程序員的話)
# NodesOfSet = the list
# elementsAndTheirNodes = the dictionary
# loop as many times as the number of key:value entries in the dictionary element:nodes
# simply: loop over all the elements
for i in range (0, len (elementsAndTheirNodes.keys())):
# there is an indent here (otherwise it wouldnt work anyway)
# loop over the tuple that serves as the value for each key for a given i-th key:value
# simply: loop over all their nodes
for j in range (0, len (elementsAndTheirNodes.values()[i])):
# test: this prints out element + 1 node and so on
# print (elementsAndTheirNodes.keys()[i], elementsAndTheirNodes.values()[i][j] )
for k in range (0, len (NodesOfSet)):
if NodesOfSet[k] == (elementsAndTheirNodes.values()[i][j]):
print (elementsAndTheirNodes.keys()[i], " is the victim")
else:
print (elementsAndTheirNodes.keys()[i], " is not the victim")
但是,這是非常耗時的,因爲它遍歷數據庫基本上一切。我可以要求幫助優化嗎?謝謝!
只是一個提示:這是不是很符合Python - >列表是一個受保護的詞,因此不要將其用作變量名,變量始終以小寫字母開頭,「詞」通常由「_」分隔。也不要在功能和「()」之間放置空格,如打印或len或範圍 – ted
您好!謝謝你的評論。我不知道在任何地方使用列表的單詞。首都的事情很有趣,雖然不是很有幫助。空格在那裏是因爲我的文本編輯器在打字時提供了我已經使用過的單詞。 – Jewenile
有人編輯我的問題讓它更容易理解,真是太好了。但是真的有必要刪除我不是程序員,我的謝意? – Jewenile