def sum_nested_list(lst1):
res = 0
for elem in lst1:
if type(elem) == list:
map(abs, elem)
res += sum_nested_list(elem)
elif type(elem) == str:
continue
else:
abs(elem)
res += elem
return res
print(sum_nested_list([1, 2, [-3, -4.5], 'abc', [5, 'abc', [-4, 0.5]]]))
#๐ Why is the absolute value not working?
38 messages ยท Page 1 of 1 (latest)
@rain monolith
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.
Closes after a period of inactivity, or when you send !close.
you're mapping but not doing anything with the result
elem = map(abs, elem)
map(abs, elem)
@rain monolith
its not working
Share the error. It's full of useful information
How do i do that
I'm sorry i'm new to all of this
Copy and paste it here
Ok
Traceback (most recent call last):
File "/Users/naderrawashdy/PycharmProjects/pythonex3/Ex3.py", line 44, in <module>
print(sum_nested_list([1, 2, [-3, -4.5], 'abc', [5, 'abc', [-4, 0.5]]]))
File "/Users/naderrawashdy/PycharmProjects/pythonex3/Ex3.py", line 36, in sum_nested_list
res += sum_nested_list(elem)
File "/Users/naderrawashdy/PycharmProjects/pythonex3/Ex3.py", line 33, in sum_nested_list
for elem in lst1:
TypeError: bad operand type for abs(): 'str'
What do you think this is telling you?
That abs doesn't work for strings
But im not using it on strings that's why im confused
you are using it on a string by mapping a list to abs
[5, 'abc', [-4, 0.5]]
!e
data = [5, 'abc', [-4, 0.5]]
print(*map(abs, data))
@blazing sky :x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 2, in <module>
003 | print(*map(abs, data))
004 | TypeError: bad operand type for abs(): 'str'
You're nearly there with the solution, you just don't need to map your lists
let the else handle abs'ing
Thank u king
np, happy to help!
I know recursion can be super confusing
(I still don't really get it either, haha)
Thanks for being patient with me
Veryyy but i think im handling well
!close
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.