SX

from collections import OrderedDict
org_data = [
[0,None,None,1],
[0,None,None,2],
[0,'pre',None,3],
[0,'pre',20,4],
[0,'pre',None,5],
[0,None,None,6],
[0,'thd',20,7],
]
tmp_d = OrderedDict()

pre_k = ""
key_index = 0
for l in org_data:
key = "_".join([str(i) for i in l[:-1] if i != None])
if key not in tmp_d:
tmp_d[key] = [l[-1]]
else:
if key == pre_k:
tmp_d[key].append(l[-1])
else:
key = key + "-" + str(key_index)
tmp_d[key] = [l[-1]]
key_index += 1
pre_k = key
print(tmp_d)

a = []
for tk in tmp_d:
if len(tk.split("")) == 1:
if len(tk.split("-")) == 1:
a.append(tmp_d[tk])
else:
a[-1].extend(tmp_d[tk])
if len(tk.split("
")) == 2:
if len(tk.split("-")) == 1:
a[-1].append(tmp_d[tk])
else:
a[-1][-1].extend(tmp_d[tk])
if len(tk.split("_")) == 3:
if len(tk.split("-")) == 1:
if type(a[-1][-1]) == int: a[-1].append([])
a[-1][-1].append(tmp_d[tk])
else:
a[-1][-1][-1].extend(tmp_d[tk])
print(a)

上一篇:Python 教程:从零到大师就是这么简单


下一篇:SpringBoot集成tk-mybatis