我在列表中获取输出
[
(u'Shift 1', u'NURSES', u'ANITHA M.N.', u'A'),
(u'Shift 1', u'Counsellors', u'SUSHMITHA B.', u'A'),
(u'Shift 2', u'Counsellors', u'ALEX THOMAS MATHEW', u'B')
]
我想将输出格式化如下。
{
name: "Shift 1",
id: "A",
expanded: true,
children: [{
name: "Counsellors",
id: "A.1",
children: [{
name: "SUSHMITHA B.",
id: "A.1.1"
}, ]
}, {
name: "Nurses",
id: "A.2",
children: [{
name: "ANITHA M.N",
id: "A.2.1"
}, ]
},
]
}, {
name: "Shift 2",
id: "B",
expanded: true,
children: [{
name: "Counsellors",
id: "B.1",
children: [{
name: "ALEX THOMAS MATHEW",
id: "B.1.1"
}, ]
},
]
},
必须完成以下格式。 在python中使用了元组列表中的多个组的概念。
from itertools
import groupby
import pdb
L = [(u 'Shift 1', u 'NURSES', u 'ANITHA. BHARATHAN', u 'A'), (u 'Shift 1', u 'NURSES', u 'ANITHA M.N.', u 'A'), (u 'Shift 1', u 'Counsellors', u 'SUSHMITHA B.', u 'A'), (u 'Shift 2', u 'Counsellors', u 'ALEX THOMAS MATHEW', u 'B')]
for x, y in groupby(sorted(L), lambda t: t[0]):
children = []
for c, d in groupby(sorted(y), lambda t: t[1]):
children.append('children: {}: {}'.format(c, ', '.join(c[2]
for c in d)))
无法获得上述格式,但接近它。任何帮助将不胜感激。