次のように、辞書を含む2つのリストがあります。
listone = [{'unit1': {'test1': 10}},
{'unit1': {'test2': 45'},
{'unit2': {'test1': 78'},
{'unit2': {'test2': 2'}}]
listtwo = [{'unit1': {'test1': 56}},
{'unit1': {'test2': 34'},
{'unit2': {'test1': 23'},
{'unit2': {'test2': 5'}}]
私はまた、すべてのユニット名とテスト名を別々のリストに持っています:
units = ['unit1', 'unit2']
testnames = ['test1,'test2']
test2
各テスト値のデルタ、つまり ( - )の val を見つけるにはどうすればよいtest1
ので、最終的に次のようにデータを配置できます。
unit1, test1, delta
unit1, test2, delta
unit2, test1, delta
unit2, test2, delta
これまでのところ、私はこれらを持っています:
def delta(array1, array2):
temp = []
temp2 = []
tmp = []
tmp2 = []
delta = []
for unit in units:
for mkey in array1:
for skey in mkey:
if skey == unit:
temp.append(mkey[skey])
floater(temp) #floats all the values
for i in testnames:
for u in temp:
tmp.append(u[i])
tmp = filter(None, tmp2)
for mkey in array2:
for skey in mkey:
if skey == unit:
temp.append(mkey[skey])
floater(temp2)
for i in testnames:
for u in temp2:
tmp2.append(u[i])
tmp2 = filter(None, tmp2)
delta = [tmp2 - tmp for tmp2, tmp in zip(tmp2, tmp)]
print delta
delta(listone,listtwo)
残念ながら、コードはKeyerror
. :(助けてください。ありがとう。