我有一个元组列表,[(falseName, realName, positionOfMistake)]
例如:
[('Milter', 'Miller', 4),
('Manton','Manson',4),
('Moller', 'Miller', 2)]
我需要写一个函数返回:
^{pr2}$我在想:
def nameStatistics(nameList):
D={}
for tup in nameList
if tup[1] not in D:
D[tup[1]]={}
if tup[0] not in D[tup[1]]:
D[tup[1]][tup[0]]=0
D[tup[1]][tup[0]] += 1
print tup[1]+":\n\t"+tup[0]
但从中我得到:
Miller:
Milter
Miller:
Moller
Manson:
Manton
你没有正确地建立你的字典,因为你的缩进在几个地方是错误的。另外,print语句不引用您构建的字典。试试这个:
您可以使用^{} 进一步简化此过程:
^{pr2}$相关问题 更多 >
编程相关推荐