How to expand a compressed list into a full list in Python? -
i have compressed list (and possibly bigger):
[[[1, [2, 3], [3, 2]]], [[2, [1, 3], [3, 1]]], [[3, [1, 2], [2, 1]]]]
what can expand complete list like?
[[1,2,3], [1,3,2], [2,1,3], [3,1,2], [2,3,1], [3,2,1]]
i think sort of recursion don't know how. thank in advance
edit: here's function wrote keeps saying syntax error.
def expandlist(alist): """expand list""" finallist = [] j in alist: if type(j) != type(list): templist = [] templist.append(j) finallist.append(templist) else: finallist.extend(expandlist(j)) return finallist
edit: whoops, meant:
[[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
not:
[[1,2,3], [1,3,2], [2,1,3], [3,1,2], [2,3,1], [3,2,1]]
sorry confusions.
you may want try this,
l = [[[1, [2, 3], [3, 2]]], [[2, [1, 3], [3, 1]]], [[3, [1, 2], [2, 1]]]] final_list = [] k in l: x in k: t = [x[0]] t.extend([i in x[1]]) final_list.append(t) t = [x[0]] t.extend([i in x[2]]) final_list.append(t) print (final_list)
this yields,
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
Comments
Post a Comment