Python function which can transverse a nested list and print out each element -
this question has answer here:
- flatten (an irregular) list of lists 35 answers
for example if have list [[['a', 'b', 'c'], ['d']],[['e'], ['f'], ['g']]]
function should print out 'a' 'b' 'c'
...ect. on separate lines.it needs able variation in list.
my_list = [[['a', 'b', 'c'], ['d']],[[[['e', ['f']]]]], ['g']] def input1(my_list): in range(0, len(my_list)): my_list2 = my_list[i] in range(0, len(my_list2)): print(my_list2[i])
my code doesnt seem work, know missing alot of whats needed neccesary function. suggestions helpful
you'll want first flatten list:
>>> import collections >>> def flatten(l): ... el in l: ... if isinstance(el, collections.iterable) , not isinstance(el, basestring): ... sub in flatten(el): ... yield sub ... else: ... yield el ... >>> l = [[['a', 'b', 'c'], ['d']],[['e'], ['f'], ['g']]] >>> item in flatten(l): ... print item ... b c d e f g
Comments
Post a Comment