#๐ How do i fix this ?
32 messages ยท Page 1 of 1 (latest)
@hybrid kayak
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
!e
def sum_nested_list(lst1):
sum=0
for elem in lst1:
print(elem)
if type(elem)==list:
sum+=sum_nested_list(elem)
if type(elem)==str:
sum+=0
else:
sum+=elem
return sum
print(sum_nested_list([1, 2, [-3, -4.5], 'abc', [5, 'abc', [-4, 0.5]]]))
@coarse wolf :x: Your 3.12 eval job has completed with return code 1.
001 | 1
002 | 2
003 | [-3, -4.5]
004 | -3
005 | -4.5
006 | Traceback (most recent call last):
007 | File "/home/main.py", line 14, in <module>
008 | print(sum_nested_list([1, 2, [-3, -4.5], 'abc', [5, 'abc', [-4, 0.5]]]))
009 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
010 | File "/home/main.py", line 10, in sum_nested_list
011 | sum+=elem
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/DVMZIRXA3F23E2MPRZYKHWAX44
Huh
the problem is the you call the function again at ```py
sum+=sum_nested_list(elem)
but after that returns you still check ```py
if type(elem)==str:
sum+=0
else:
sum+=elem
and ```py
sum+=elem
is runed with the list
change ```py
if type(elem)==str:
to: ```py
elif type(elem)==str:
also there are some stuff to make you code look better and probably faster
are you interested in it?
Go ahead im always interested
firstly make the tabbing consistent throughtout the code:
if type(elem)==list:
sum+=sum_nested_list(elem)
elif type(elem)==str:
sum+=0
else:
sum+=elem
then you could do isinstance(elem,list) instead of type == list (you also could do it with any datatype)
don't name your variables to and already existing function (like sum)
instead of ```py
elif type(elem)==str:
sum+=0
you could do: ```py
elif type(elem)==str:
continue
(continue makes the for cycle step to the next iteration without running anything after continue)
Or just you could skip the mayhem
def flatten(S):
"""Flatten a list recursively to a 1-D list
https://stackoverflow.com/a/12472564
"""
if S == []:
return S
if isinstance(S[0], list):
return flatten(S[0]) + flatten(S[1:])
return S[:1] + flatten(S[1:])
def sum_nested_list(lst1):
return sum(i for i in flatten(lst1) if not isinstance(i, str))
Pinch code from StackOverflow, step 1 to be a programmer
You can also do a lil functional programming
def sum_nested_list(l: list):
result = 0
result += sum(n for n in l if type(n)==int)
result += sum(sum_nested_list(t) for t in l if type(t)==list)
return result
Lol
why typehint the input and not typehint the output?
Can you explain what you did here
Something like pylance will catch that this returns an int, but it won't know that l is a list if you don't type hint that
n for n in l if type(n)==int gives you all the ints in the current list, and we sum that
t for t in l if type(t)==list gives you all the lists, so we want to add together all the sum_nested_list(t)
wow I didn't know that one
Oh so u didn't use recursion at all
No I did, here
def sum_nested_list(l: list):
result = 0
result += sum(n for n in l if type(n)==int)
result += sum(sum_nested_list(t) for t in l if type(t)==list) # <--
return result
Oh i see
https://www.w3schools.com/python/python_lists_comprehension.asp
check out list comprehension if you dont know whats going on
newlist = [expression for item in iterable if condition == True]
!e
you could also use an accumulator:
def nested_sum(lst, acc=0):
if not lst:
return acc
if isinstance(lst[0], list):
acc += nested_sum(lst[0])
if isinstance(lst[0], int):
acc += lst[0]
return nested_sum(lst[1:], acc)
print(nested_sum([1,2, [2,3, "test",4], "a", [4, "kl", 5], 2, [5, "hj", 7, [6, 9, [1, 2]]]]))
@tacit rock :white_check_mark: Your 3.12 eval job has completed with return code 0.
53
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.