python - Converts old style lists to the new easier to understand format -
the old list format looked like:
['item 1', [['item 1.1', []], ['item 1.2', []]]] and supposed converted to:
['item 1', ['item 1.1', 'item 1.2']] this question comes django's ource code have trouble figuring out.thanks in advance.
another option... keep format request, , should work number of sub-items, since lack example more complex sub-items, don't know if hold real data... you'd have check!
test = ['item 1', [['item 1.1', []], ['item 1.2', []]]] def convert(oldlist, newlist): in range(len(oldlist)): if type(oldlist[i]) == list , type(oldlist[i][0]) != list: newlist.append(oldlist[i][0]) elif type(oldlist[i]) != list: newlist.append(oldlist[i]) else: newlist.append([]) convert(oldlist[i], newlist[-1]) return newlist print(convert(test, [])) output is:
['item 1', ['item 1.1', 'item 1.2']]
Comments
Post a Comment