我写了一个简单的脚本,它的范围是:如何获取其值至少包含一个项目在另一个列表中的字典键?
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