0

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 dataenter image description here

Zepol
  • 195
  • 2
  • 3
  • 14

2 Answers2

0

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>'
neox
  • 81
  • 6
  • maybe I did not explain myself correctly. I would like to sort it not by the first item of the tuple but by a correlation of the third item(parent) with the first item(ID). For example, if the parent equals 111, it should be ordered next to the tuple with the ID number 111. In this case what you did works, but in the real world those ID numbers will not be in order. – Zepol May 11 '17 at 21:31
  • in that case you can hash them or build a tree and pull from there instead of the `list_of_tuples`. Sort first, build later –  May 11 '17 at 21:35
  • @monchitos82 and how will I build this? – Zepol May 11 '17 at 21:52
  • here you can pick some good ideas: * http://stackoverflow.com/questions/2598437/how-to-implement-a-binary-tree * http://stackoverflow.com/questions/4174941/how-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list * http://stackoverflow.com/questions/9858096/creating-a-dictionary-with-list-of-lists-in-python * http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value So, you could: 1 use `sorted` + `itemgetter` to do the job (i favor this, second URL), 2. insert to a dictionary and sort keys or 3. make a binary tree and insert by the x position –  May 11 '17 at 23:04
  • If my last comment is not helpful/clear: in the example above replace the `for` line with this `for row in sorted(list_of_tuples, key=itemgetter(2)):` and include the import at top: `from operator import itemgetter` –  May 11 '17 at 23:11
0

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!

Yasin Yousif
  • 969
  • 7
  • 23