字典的蟒蛇字典 [英] python dictionary of dictionaries
本文介绍了字典的蟒蛇字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从另一个函数中,我有这样的元组:这个('falseName','realName',positionOfMistake)
('Milter','Miller',4)
。
我需要编写一个使这样的字典的函数:
From another function, I have tuples like this ('falseName', 'realName', positionOfMistake)
, eg. ('Milter', 'Miller', 4)
.
I need to write a function that make a dictionary like this:
D={realName:{falseName:[positionOfMistake], falseName:[positionOfMistake]...},
realName:{falseName:[positionOfMistake]...}...}
该函数必须使用上面的字典和元组作为参数。
The function has to take a dictionary and a tuple like above, as arguments.
我在想这样开始:
def addToNameDictionary(d, tup):
dictionary={}
tup=previousFunction(string)
for element in tup:
if not dictionary.has_key(element[1]):
dictionary.append(element[1])
elif:
if ...
但它不工作,我在这里坚持。 >
But it is not working and I am kind of stucked here.
推荐答案
如果只是添加一个新的元组,你确定内部字典中没有碰撞,你可以这样做: / p>
If it is only to add a new tuple and you are sure that there is no collisions in the inner dictionary you can do this:
def addNameToDictionary(d, tup):
if tup[0] not in d:
d[tup[0]] = {}
d[tup[0]][tup[1]] = [tup[2]]
这篇关于字典的蟒蛇字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文