0

How to print this mixed nested list line by line?

n = [1,2,3,4,5,[1,2,3,4,5]]

29th May 2019, 12:04 PM
Aakash Gupta
Aakash Gupta - avatar
9 ответов
+ 3
def fun(l): for x in l: if(type(x) is list): print() fun(x) else: print(x, end = " ") n = [1,2,3,4,5,[1,2,3,4,5]] fun(n)
29th May 2019, 12:32 PM
Ipang
+ 2
You only call nested_sum(x) but didn't add the return value from nested_sum function into variable <t>, this is why you got 15 as result. n = [1,2,[1,2,3,4,5],[1,2,3,4,5],3,4,5] def nested_sum(n): t = 0 for x in n: if(type(x) is list): # add nested_sum return value to <t> t += nested_sum(x) else: t += x return t print(nested_sum(n)) Hth, cmiiw
31st May 2019, 7:20 AM
Ipang
+ 1
Thanks a lot..
29th May 2019, 1:56 PM
Aakash Gupta
Aakash Gupta - avatar
+ 1
If I am trying to add all numbers through this code works fine.. but if there are more list than 1 it is not working then why ? 🤔
30th May 2019, 12:00 PM
Aakash Gupta
Aakash Gupta - avatar
+ 1
Aakash Gupta , Can you share your code link? I can't say anything until I have a look at least ...
30th May 2019, 5:03 PM
Ipang
+ 1
🤗 thanks a lot.. give me some tips to improve my thinking.. I always stuck ..
31st May 2019, 12:01 PM
Aakash Gupta
Aakash Gupta - avatar
+ 1
You're welcome, I'm not even sure what to say as tips, I've learned from making mistakes, and from seeing how other people do something, and ask them when there's something I don't understand (quite often happens). Time will make you grow, as long as you keep on learning I guess 👍
31st May 2019, 1:01 PM
Ipang
+ 1
I got it.. thanks ☺️
1st Jun 2019, 8:08 AM
Aakash Gupta
Aakash Gupta - avatar
0
n = [1,2,[1,2,3,4,5],[1,2,3,4,5],3,4,5] def nested_sum(n): t = 0 for x in n: if(type(x) is list): nested_sum(x) else: t+=x return t print(nested_sum(n)) #Total is 45 but giving 15
31st May 2019, 6:59 AM
Aakash Gupta
Aakash Gupta - avatar