简体   繁体   中英

how to sort a list by hierarchy and make an html table out of it in python?

I am trying to sort and make an html table from a python list (list comes from database):

[('222', 'Workroom', '111'),
 ('333', 'Setup Part', '222'),
 ('444', 'Scale', '222'),
 ('666', 'Workroom', ''),
 ('888', 'Setup Part', '777'),
 ('777', 'Workroom', '666'),
 ('555', 'Workroom', '111'),
 ('111', 'Workroom', '')]

based on their hierarchy. The first item in each tuple represents its ID, the second one represents a description and the third represents its "parent". How could I make a program that organizes it in a hierarchical form in an html table?

this is what I mean by hierarchical form and an example of what I would like to do with the data 在此处输入图片说明

Well if by hierarchical order you mean sorting it by the first value of the tuple, you could just use the command sorted() in python. This would be the output:

[('111', 'Workroom', ''), ('222', 'Workroom', '111'), ('333', 'Setup Part', '222'), ('444', 'Scale', '222'), ('555', 'Workroom', '111'), ('666', 'Workroom', ''), ('777', 'Workroom', '666'), ('888', 'Setup Part', '777')]

Now you can create a html table out of this with python:

head = '\n<tr>\n\t<th>A</th>\n\t<th>B</th>\n\t<th>C</th>\n</tr>\n'
body = ''
for row in sorted(list_of_tuples):
    body += '<tr>\n\t<td>{}</td>\n\t<td>{}</td>\n\t<td>{}</td>\n</tr>\n'.format(row[0], row[1], row[2])

table = '<table>' + head + body + '</table>'

Ok , if we say that the parent is always bigger than the children /because it is above them/ we write:

a = [('222', 'Workroom', '111'),
('333', 'Setup Part', '222'),
('444', 'Scale', '222'),
('666', 'Workroom', ''),
('888', 'Setup Part', '777'),
('777', 'Workroom', '666'),
('555', 'Workroom', '111'),
('111', 'Workroom', '')]

for i,s in enumerate(a):
    if len(s[2])==0:
        a[i] =(s[0],s[1],'000')
        # just to avoid int error

v = sorted(a, key=lambda x: x[0]+str(int(x[0])-int(x[2])))
print v

which gives:

[('111', 'Workroom', '000'),
('222', 'Workroom', '111'),
('333', 'Setup Part', '222'),
('444', 'Scale', '222'),
('555', 'Workroom', '111'),
('666', 'Workroom', '000'),
('777', 'Workroom', '666'),
('888', 'Setup Part', '777')]

Now , just to know the levels , we can nest lists:

z = [];

for r in v:
    x = r[:];
    for n in range(int(r[2][0])):
        x = list([x])
    z.append(x)

# Result:

[('111', 'Workroom', '000'),
 [('222', 'Workroom', '111')],
 [[('333', 'Setup Part', '222')]],
 [[('444', 'Scale', '222')]],
 [('555', 'Workroom', '111')],
 ('666', 'Workroom', '000'),
 [[[[[[('777', 'Workroom', '666')]]]]]],
 [[[[[[[('888', 'Setup Part', '777')]]]]]]]]

Now , to make this html is an easy job:

just , put each element in <td> </td> each list you find,

perhaps check if the length is 3 item is found ==> close the <td> tags!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM