【发布时间】:2011-03-21 16:22:04
【问题描述】:
另一个列表字典问题。
我有一个列表中包含单元名称和测试名称的字典,如下所示:
dictA = {('unit1', 'test1'): 10, ('unit2', 'test1'): 78, ('unit2', 'test2'): 2, ('unit1', 'test2'): 45}
units = ['unit1', 'unit2']
testnames = ['test1','test2']
我们如何在 testnames 中找到每个测试的最大值:
我尝试如下:
def max(dict, testnames_array):
maxdict = {}
maxlist = []
temp = []
for testname in testnames_array:
for (xunit, xtestname), value in dict.items():
if xtestname == testname:
if not isinstance(value, str):
temp.append(value)
temp = filter(None, temp)
stats = corestats.Stats(temp)
k = stats.max() #finds the max of a list using another module
maxdict[testname] = k
maxlist.append(maxdict)
maxlist.insert(0,{'Type':'MAX'})
return maxlist
现在的问题是我得到了输出:
[{'Type':'MAX'}, {'test1': xx}, {'test2':xx}]
其中 xx 都作为相同的值返回!!
我的错在哪里? 有更简单的方法吗? 请指教。谢谢。
【问题讨论】:
标签: python list dictionary max tuples