+ 2

Help recursive function (solved)

Goal: To flatten list : a= [1, [1,2,3], 2], [1,2] My function: def recusive_flat(x): r = [] for m in x: if m is iter: recursive_flat(m): else: r.append(int(m)) My goal is to loop through until every value is added as an integer.

21st Jul 2020, 8:08 PM
madeline
madeline - avatar
3 Réponses
+ 5
My fix: Check existence of "__iter__" method. https://code.sololearn.com/cd8oFew3A8y9/?ref=app
21st Jul 2020, 8:30 PM
李立威
+ 4
madeline First of, the a you provided is a Tuple, but since you said flatten list, I corrected it to a list. So: a = [1, [1,2,3], 2, [1,2]] def sum_it(x): flatten = [i if type(i)==int else sum(i) for i in a] return sum(flatten) print(sum_it(a))
21st Jul 2020, 8:32 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
thank you.
21st Jul 2020, 8:35 PM
madeline
madeline - avatar