所以我正在python 3.6中编写一些代码,我有一个字典,如果'jam'和'cell'具有相同的值,我想对output_jam和output_ot求和。这是我的代码。任何人都可以帮助我修复我的代码
data = {
1: {'cell_1': ['13a'], 'jam_1': ['07-08'], 'model_1': ['SUPERSTAR'], 'output_1': ['10'], 'output_jam_1': [''],
'time_1': [''], 'output_ot_1': [''], 'time_ot_1': ['']}
,
2: {'cell_2': ['13a'], 'jam_2': ['07-08'], 'model_2': ['SUPERSTAR'], 'output_2': ['20'], 'output_jam_2': [''],
'time_2': [''], 'output_ot_2': [''], 'time_ot_2': ['']}
,
3: {'cell_3': ['13a'], 'jam_3': ['07-08'], 'model_3': ['SUPERSTAR'], 'output_3': ['40'], 'output_jam_3': [''],
'time_3': [''], 'output_ot_3': [''], 'time_ot_3': ['']}
,
4: {'cell_4': ['13b'], 'jam_4': ['08-09'], 'model_4': ['SUPERSTAR'], 'output_4': ['30'], 'output_jam_4': [''],
'time_4': [''], 'output_ot_4': [''], 'time_ot_4': ['']}
,
5: {'cell_5': ['13d'], 'jam_5': ['16-17'], 'model_5': ['SUPERSTAR'], 'output_5': ['40'], 'output_jam_5': [''],
'time_5': [''], 'output_ot_5': [''], 'time_ot_5': ['']}
,
6: {'cell_6': ['13d'], 'jam_6': ['16-17'], 'model_6': ['SUPERSTAR'], 'output_6': ['40'], 'output_jam_6': [''],
'time_6': [''], 'output_ot_6': [''], 'time_ot_6': ['']}
,
7: {'cell_7': ['13d'], 'jam_7': ['16-17'], 'model_7': ['SUPERSTAR'], 'output_7': ['10'], 'output_jam_7': [''],
'time_7': [''], 'output_ot_7': [''], 'time_ot_7': ['']}
,
8: {'cell_8': ['13d'], 'jam_8': ['18-19'], 'model_8': ['SUPERSTAR'], 'output_8': ['60'], 'output_jam_8': [''],
'time_8': [''], 'output_ot_8': [''], 'time_ot_8': ['']}
, }
output_ = 'output_'
output_jam_ = 'output_jam_'
output_ot_ = 'output_ot_'
time_ = 'time_'
time_ot_ = 'time_ot_'
cell_ = 'cell_'
jam_ = 'jam_'
cek_jam = [['16-17'], ['17-18'], ['18-19']]
for i in range(1, len(data) + 1, 1):
cek_data_cell = data[i][cell_ + str(i)]
cek_data_jam = data[i][jam_ + str(i)]
for ii in range(1, len(data) + 1, 1):
for iii in range(1, len(data) +1, 1):
if data[i] != data[ii] and data[ii] != data[iii]:
if cek_data_cell == data[ii][cell_ + str(ii)] == data[iii][cell_+str(iii)] and cek_data_jam == data[ii][jam_ + str(ii)] == data[iii][jam_ + str(iii)]:
if cek_data_jam not in cek_jam:
data[i][output_jam_ + str(i)] = [str(int(data[i][output_ + str(i)][0]) + int(data[ii][output_ + str(ii)][0])+ int(data[iii][output_ + str(iii)][0]))]
else:
data[i][output_ot_ + str(i)] = [str(int(data[i][output_ + str(i)][0]) + int(data[ii][output_ + str(ii)][0])+ int(data[iii][output_ + str(iii)][0]))]
elif cek_data_cell == data[ii][cell_ + str(ii)] and cek_data_jam == data[ii][jam_ + str(ii)]:
if cek_data_jam not in cek_jam:
data[i][output_jam_ + str(i)] = [str(int(data[i][output_ + str(i)][0]) + int(data[ii][output_ + str(ii)][0]))]
else:
data[i][output_ot_ + str(i)] = [str(int(data[i][output_ + str(i)][0]) + int(data[ii][output_ + str(ii)][0]))]
elif cek_data_cell== data[iii][cell_+str(iii)] and cek_data_jam == data[iii][jam_ + str(iii)]:
if cek_data_jam not in cek_jam:
data[i][output_jam_ + str(i)] = [str(int(data[i][output_ + str(i)][0]) + int(data[iii][output_ + str(iii)][0]))]
else:
data[i][output_ot_ + str(i)] = [str(int(data[i][output_ + str(i)][0]) + int(data[iii][output_ + str(iii)][0]))]
else:
if cek_data_jam in cek_jam:
if data[i][output_ot_ + str(i)] == ['']:
data[i][output_ot_ + str(i)] = data[i][output_ + str(i)]
else:
if data[i][output_jam_ + str(i)] == ['']:
data[i][output_jam_ + str(i)] = data[i][output_ + str(i)]
try:
data[i][time_ + str(i)] = [
str(round(int(data[i][output_ + str(i)][0]) / int(data[i][output_jam_ + str(i)][0]), 2))]
except:
pass
try:
data[i][time_ot_ + str(i)] = [
str(round(int(data[i][output_ + str(i)][0]) / int(data[i][output_ot_ + str(i)][0]), 2))]
except:
pass
for i in data:
print (data[i])
这是错误结果,因为output_jam_1中的正确值是70(10 + 20 + 40)而不是50。output_jam_2中的正确值是70(10 + 20 + 40)而不是60。output_jam_3中的正确值是70(10+ 20 + 40)而不是80
{'cell_1': ['13a'], 'jam_1': ['07-08'], 'model_1': ['SUPERSTAR'], 'output_1': ['10'], 'output_jam_1': ['50'], 'time_1': ['0.2'], 'output_ot_1': [''], 'time_ot_1': ['']}
{'cell_2': ['13a'], 'jam_2': ['07-08'], 'model_2': ['SUPERSTAR'], 'output_2': ['20'], 'output_jam_2': ['60'], 'time_2': ['0.33'], 'output_ot_2': [''], 'time_ot_2': ['']}
{'cell_3': ['13a'], 'jam_3': ['07-08'], 'model_3': ['SUPERSTAR'], 'output_3': ['40'], 'output_jam_3': ['80'], 'time_3': ['0.5'], 'output_ot_3': [''], 'time_ot_3': ['']}
{'cell_4': ['13b'], 'jam_4': ['08-09'], 'model_4': ['SUPERSTAR'], 'output_4': ['30'], 'output_jam_4': ['60'], 'time_4': ['0.5'], 'output_ot_4': [''], 'time_ot_4': ['']}
{'cell_5': ['13d'], 'jam_5': ['16-17'], 'model_5': ['SUPERSTAR'], 'output_5': ['40'], 'output_jam_5': [''], 'time_5': [''], 'output_ot_5': ['50'], 'time_ot_5': ['0.8']}
{'cell_6': ['13d'], 'jam_6': ['16-17'], 'model_6': ['SUPERSTAR'], 'output_6': ['40'], 'output_jam_6': [''], 'time_6': [''], 'output_ot_6': ['50'], 'time_ot_6': ['0.8']}
{'cell_7': ['13d'], 'jam_7': ['16-17'], 'model_7': ['SUPERSTAR'], 'output_7': ['10'], 'output_jam_7': [''], 'time_7': [''], 'output_ot_7': ['20'], 'time_ot_7': ['0.5']}
{'cell_8': ['13d'], 'jam_8': ['18-19'], 'model_8': ['SUPERSTAR'], 'output_8': ['60'], 'output_jam_8': [''], 'time_8': [''], 'output_ot_8': ['120'], 'time_ot_8': ['0.5']}
可以实现相同代码的简便最佳方法吗? 我的预期和实际结果,
{'cell_1': ['13a'], 'jam_1': ['07-08'], 'model_1': ['SUPERSTAR'], 'output_1': ['10'], 'output_jam_1': ['70'], 'time_1': ['0.33'], 'output_ot_1': [''], 'time_ot_1': ['']}
{'cell_2': ['13a'], 'jam_2': ['07-08'], 'model_2': ['SUPERSTAR'], 'output_2': ['20'], 'output_jam_2': ['70'], 'time_2': ['0.5'], 'output_ot_2': [''], 'time_ot_2': ['']}
{'cell_3': ['13a'], 'jam_3': ['07-08'], 'model_3': ['SUPERSTAR'], 'output_3': ['40'], 'output_jam_3': ['70'], 'time_3': ['0.5'], 'output_ot_3': [''], 'time_ot_3': ['']}
{'cell_4': ['13b'], 'jam_4': ['08-09'], 'model_4': ['SUPERSTAR'], 'output_4': ['30'], 'output_jam_4': ['30'], 'time_4': ['0.5'], 'output_ot_4': [''], 'time_ot_4': ['']}
{'cell_5': ['13d'], 'jam_5': ['16-17'], 'model_5': ['SUPERSTAR'], 'output_5': ['40'], 'output_jam_5': [''], 'time_5': [''], 'output_ot_5': ['90'], 'time_ot_5': ['0.8']}
{'cell_6': ['13d'], 'jam_6': ['16-17'], 'model_6': ['SUPERSTAR'], 'output_6': ['40'], 'output_jam_6': [''], 'time_6': [''], 'output_ot_6': ['90'], 'time_ot_6': ['0.8']}
{'cell_7': ['13d'], 'jam_7': ['16-17'], 'model_7': ['SUPERSTAR'], 'output_7': ['10'], 'output_jam_7': [''], 'time_7': [''], 'output_ot_7': ['90'], 'time_ot_7': ['0.5']}
{'cell_8': ['13d'], 'jam_8': ['18-19'], 'model_8': ['SUPERSTAR'], 'output_8': ['60'], 'output_jam_8': [''], 'time_8': [''], 'output_ot_8': ['60'], 'time_ot_8': ['0.5']}