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

2
  • 1
    What do you mean by "hierarchical form"? Commented May 11, 2017 at 21:11
  • @vishes_shell I have made an edit to answer your question Commented May 11, 2017 at 21:25

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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>'

5 Comments

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.
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
@monchitos82 and how will I build this?
here you can pick some good ideas: * stackoverflow.com/questions/2598437/… * stackoverflow.com/questions/4174941/… * stackoverflow.com/questions/9858096/… * stackoverflow.com/questions/613183/… 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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.