我有下面的字典
#original
data = {
'cell_0': ['13a'], 'jam_0': ['07-08'], 'model_0': ['SUPERSTAR'], 'output_0': ['10'], 'output_jam_0': [''], 'time_0': [''], 'output_ot_0': [''], 'time_ot_0': [''],
'cell_1': ['13a'], 'jam_1': ['07-08'], 'model_1': ['SUPERSTAR'], 'output_1': ['20'], 'output_jam_1': [''], 'time_1': [''], 'output_ot_1': [''], 'time_ot_1': [''],
'cell_2': ['13c'], 'jam_2': ['07-08'], 'model_2': ['SUPERSTAR'], 'output_2': ['40'], 'output_jam_2': [''], 'time_2': [''], 'output_ot_2': [''], 'time_ot_2': [''],
'cell_3': ['13b'], 'jam_3': ['08-09'], 'model_3': ['SUPERSTAR'], 'output_3': ['30'], 'output_jam_3': [''], 'time_3': [''], 'output_ot_3': [''], 'time_ot_3': [''],
'cell_4': ['13d'], 'jam_4': ['16-17'], 'model_4': ['SUPERSTAR'], 'output_4': ['40'], 'output_jam_4': [''], 'time_4': [''], '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': [''], 'time_ot_5': [''],
'cell_6': ['13d'], 'jam_6': ['17-18'], 'model_6': ['SUPERSTAR'], 'output_6': ['10'], 'output_jam_6': [''], 'time_6': [''], 'output_ot_6': [''], 'time_ot_6': [''],
'cell_7': ['13d'], 'jam_7': ['18-19'], 'model_7': ['SUPERSTAR'], 'output_7': ['60'], 'output_jam_7': [''], 'time_7': [''], 'output_ot_7': [''], 'time_ot_7': [''],
}
我想将我的字典与另一本字典分开。 我希望输出为:
#output
data = {
1: {'cell_0': ['13a'], 'jam_0': ['07-08'], 'model_0': ['SUPERSTAR'], 'output_0': ['10'], 'output_jam_0': [''], 'time_0': [''], 'output_ot_0': [''], 'time_ot_0': ['']},
2: {'cell_1': ['13a'], 'jam_1': ['07-08'], 'model_1': ['SUPERSTAR'], 'output_1': ['20'], 'output_jam_1': [''], 'time_1': [''], 'output_ot_1': [''], 'time_ot_1': ['']},
3: {'cell_2': ['13c'], 'jam_2': ['07-08'], 'model_2': ['SUPERSTAR'], 'output_2': ['40'], 'output_jam_2': [''], 'time_2': [''], 'output_ot_2': [''], 'time_ot_2': ['']},
4: {'cell_3': ['13b'], 'jam_3': ['08-09'], 'model_3': ['SUPERSTAR'], 'output_3': ['30'], 'output_jam_3': [''], 'time_3': [''], 'output_ot_3': [''], 'time_ot_3': ['']},
5: {'cell_4': ['13d'], 'jam_4': ['16-17'], 'model_4': ['SUPERSTAR'], 'output_4': ['40'], 'output_jam_4': [''], 'time_4': [''], 'output_ot_4': [''], 'time_ot_4': ['']},
6: {'cell_5': ['13d'], 'jam_5': ['16-17'], 'model_5': ['SUPERSTAR'], 'output_5': ['40'], 'output_jam_5': [''], 'time_5': [''], 'output_ot_5': [''], 'time_ot_5': ['']},
7: {'cell_6': ['13d'], 'jam_6': ['17-18'], 'model_6': ['SUPERSTAR'], 'output_6': ['10'], 'output_jam_6': [''], 'time_6': [''], 'output_ot_6': [''], 'time_ot_6': ['']},
8: {'cell_7': ['13d'], 'jam_7': ['18-19'], 'model_7': ['SUPERSTAR'], 'output_7': ['60'], 'output_jam_7': [''], 'time_7': [''], 'output_ot_7': [''], 'time_ot_7': ['']},
}
如何将输出字典转换为原始
#convert to original again
data = {
'cell_0': ['13a'], 'jam_0': ['07-08'], 'model_0': ['SUPERSTAR'], 'output_0': ['10'], 'output_jam_0': [''], 'time_0': [''], 'output_ot_0': [''], 'time_ot_0': [''],
'cell_1': ['13a'], 'jam_1': ['07-08'], 'model_1': ['SUPERSTAR'], 'output_1': ['20'], 'output_jam_1': [''], 'time_1': [''], 'output_ot_1': [''], 'time_ot_1': [''],
'cell_2': ['13c'], 'jam_2': ['07-08'], 'model_2': ['SUPERSTAR'], 'output_2': ['40'], 'output_jam_2': [''], 'time_2': [''], 'output_ot_2': [''], 'time_ot_2': [''],
'cell_3': ['13b'], 'jam_3': ['08-09'], 'model_3': ['SUPERSTAR'], 'output_3': ['30'], 'output_jam_3': [''], 'time_3': [''], 'output_ot_3': [''], 'time_ot_3': [''],
'cell_4': ['13d'], 'jam_4': ['16-17'], 'model_4': ['SUPERSTAR'], 'output_4': ['40'], 'output_jam_4': [''], 'time_4': [''], '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': [''], 'time_ot_5': [''],
'cell_6': ['13d'], 'jam_6': ['17-18'], 'model_6': ['SUPERSTAR'], 'output_6': ['10'], 'output_jam_6': [''], 'time_6': [''], 'output_ot_6': [''], 'time_ot_6': [''],
'cell_7': ['13d'], 'jam_7': ['18-19'], 'model_7': ['SUPERSTAR'], 'output_7': ['60'], 'output_jam_7': [''], 'time_7': [''], 'output_ot_7': [''], 'time_ot_7': [''],
}
答案 0 :(得分:3)
无需使用库,您可以通过以下方式将原始字典转换为输出字典:
result_1 = {}
for k, v in data.items():
number = int(k[-1])
if number not in result_1:
result_1[number] = {}
result_1[number][k] = v
然后将其转换回
result_2 = {}
for v in result_1.values():
result_2.update(v)
答案 1 :(得分:2)
您可以使用itertools.groupby
:
import itertools, re
data = {'cell_0': ['13a'], 'jam_0': ['07-08'], 'model_0': ['SUPERSTAR'], 'output_0': ['10'], 'output_jam_0': [''], 'time_0': [''], 'output_ot_0': [''], 'time_ot_0': [''], 'cell_1': ['13a'], 'jam_1': ['07-08'], 'model_1': ['SUPERSTAR'], 'output_1': ['20'], 'output_jam_1': [''], 'time_1': [''], 'output_ot_1': [''], 'time_ot_1': [''], 'cell_2': ['13c'], 'jam_2': ['07-08'], 'model_2': ['SUPERSTAR'], 'output_2': ['40'], 'output_jam_2': [''], 'time_2': [''], 'output_ot_2': [''], 'time_ot_2': [''], 'cell_3': ['13b'], 'jam_3': ['08-09'], 'model_3': ['SUPERSTAR'], 'output_3': ['30'], 'output_jam_3': [''], 'time_3': [''], 'output_ot_3': [''], 'time_ot_3': [''], 'cell_4': ['13d'], 'jam_4': ['16-17'], 'model_4': ['SUPERSTAR'], 'output_4': ['40'], 'output_jam_4': [''], 'time_4': [''], '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': [''], 'time_ot_5': [''], 'cell_6': ['13d'], 'jam_6': ['17-18'], 'model_6': ['SUPERSTAR'], 'output_6': ['10'], 'output_jam_6': [''], 'time_6': [''], 'output_ot_6': [''], 'time_ot_6': [''], 'cell_7': ['13d'], 'jam_7': ['18-19'], 'model_7': ['SUPERSTAR'], 'output_7': ['60'], 'output_jam_7': [''], 'time_7': [''], 'output_ot_7': [''], 'time_ot_7': ['']}
new_data = sorted(data.items(), key=lambda x:int(re.findall('\d+$', x[0])[0]))
r = {a+1:dict(list(b)) for a, b in itertools.groupby(new_data, key=lambda x:int(re.findall('\d+$', x[0])[0]))}
输出:
{1: {'cell_0': ['13a'], 'jam_0': ['07-08'], 'model_0': ['SUPERSTAR'], 'output_0': ['10'], 'output_jam_0': [''], 'time_0': [''], 'output_ot_0': [''], 'time_ot_0': ['']},
2: {'cell_1': ['13a'], 'jam_1': ['07-08'], 'model_1': ['SUPERSTAR'], 'output_1': ['20'], 'output_jam_1': [''], 'time_1': [''], 'output_ot_1': [''], 'time_ot_1': ['']},
3: {'cell_2': ['13c'], 'jam_2': ['07-08'], 'model_2': ['SUPERSTAR'], 'output_2': ['40'], 'output_jam_2': [''], 'time_2': [''], 'output_ot_2': [''], 'time_ot_2': ['']},
4: {'cell_3': ['13b'], 'jam_3': ['08-09'], 'model_3': ['SUPERSTAR'], 'output_3': ['30'], 'output_jam_3': [''], 'time_3': [''], 'output_ot_3': [''], 'time_ot_3': ['']},
5: {'cell_4': ['13d'], 'jam_4': ['16-17'], 'model_4': ['SUPERSTAR'], 'output_4': ['40'], 'output_jam_4': [''], 'time_4': [''], 'output_ot_4': [''], 'time_ot_4': ['']},
6: {'cell_5': ['13d'], 'jam_5': ['16-17'], 'model_5': ['SUPERSTAR'], 'output_5': ['40'], 'output_jam_5': [''], 'time_5': [''], 'output_ot_5': [''], 'time_ot_5': ['']},
7: {'cell_6': ['13d'], 'jam_6': ['17-18'], 'model_6': ['SUPERSTAR'], 'output_6': ['10'], 'output_jam_6': [''], 'time_6': [''], 'output_ot_6': [''], 'time_ot_6': ['']},
8: {'cell_7': ['13d'], 'jam_7': ['18-19'], 'model_7': ['SUPERSTAR'], 'output_7': ['60'], 'output_jam_7': [''], 'time_7': [''], 'output_ot_7': [''], 'time_ot_7': ['']}}