Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 calculation with list in list gianniskampanakis Programmer named Tim Posts: 11 Threads: 2 Joined: Aug 2019 Reputation: 0 Likes received: 0 #1 Aug-08-2019, 06:36 PM (This post was last modified: Aug-08-2019, 08:37 PM by Yoriz. Edited 2 times in total. Edit Reason: Added code tags ) hello. with this code i can calculate 1 + 2 + 3 = 6 ```l = [1,2,3,] def suml(l): s= 0 for i in l: s = s + i return s ``` but what can i do if i want to calculate this list ( 1 + 2 +3 +( 1 + 2) = 9) `l = [1,2,3,[1,2]]` Yoriz wrote Aug-08-2019, 08:37 PM:Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time. ndc85430 Verb Conjugator Posts: 527 Threads: 0 Joined: Apr 2017 Reputation: 50 Likes received: 112 #2 Aug-08-2019, 06:37 PM What have you thought about, or tried? gianniskampanakis Programmer named Tim Posts: 11 Threads: 2 Joined: Aug 2019 Reputation: 0 Likes received: 0 #3 Aug-08-2019, 06:43 PM Somehow I have to use the def inside the def but I cant solve it . Or call the def repeatedly wavic So-and-so of the Yard Posts: 2,898 Threads: 39 Joined: Sep 2016 Reputation: 89 Likes received: 366 #4 Aug-08-2019, 07:12 PM (This post was last modified: Aug-09-2019, 06:10 AM by wavic. Edited 1 time in total.) You can try to flatten the nested lists first. There is a topic here about that. "As they say in Mexico 'dosvidaniya'. That makes two vidaniyas." https://freedns.afraid.org gianniskampanakis Programmer named Tim Posts: 11 Threads: 2 Joined: Aug 2019 Reputation: 0 Likes received: 0 #5 Aug-08-2019, 07:41 PM Well I need the code to work for both lists. It has to calculate every list that you can give . Yoriz Posts: 1,250 Threads: 28 Joined: Sep 2016 Reputation: 120 Likes received: 283 #6 Aug-08-2019, 08:44 PM (Aug-08-2019, 06:43 PM)gianniskampanakis Wrote: Somehow I have to use the def inside the def but I cant solve it . Or call the def repeatedly Do you have to use Recursion ? gianniskampanakis Programmer named Tim Posts: 11 Threads: 2 Joined: Aug 2019 Reputation: 0 Likes received: 0 #7 Aug-08-2019, 09:04 PM (This post was last modified: Aug-08-2019, 09:36 PM by gianniskampanakis.) No recursion is not needed. I study this exercise in order to give exams at university. Theoretically we have not learn yet recursion The help note says: Write a sumL function that will calculate the sum of the elements of a list of numbers and call this function repeatedly.] cvsae Silly Frenchman Posts: 28 Threads: 3 Joined: Jul 2019 Reputation: 3 Likes received: 2 #8 Aug-08-2019, 11:19 PM (This post was last modified: Aug-08-2019, 11:22 PM by cvsae. Edited 1 time in total.) Γεια σου γιαννη ```l = [1,2,3,[1,2]] def suml(l, s=0): for i in l: if type(i) == int: s +=i else: return suml(i, s) return s``````l = [1,2,3,[1,2]] def suml(l): s = 0 for i in l: if type(i) == int: s +=i else: for t in i: s +=t return s``` wavic So-and-so of the Yard Posts: 2,898 Threads: 39 Joined: Sep 2016 Reputation: 89 Likes received: 366 #9 Aug-09-2019, 06:11 AM This looks like recursion "As they say in Mexico 'dosvidaniya'. That makes two vidaniyas." https://freedns.afraid.org gianniskampanakis Programmer named Tim Posts: 11 Threads: 2 Joined: Aug 2019 Reputation: 0 Likes received: 0 #10 Aug-09-2019, 06:39 AM Σε ευχαριστώ το type(i) εκτός από int τι τιμες μπορεί να πάρει στην περίπτωση μας ; « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Add items from one list to list of lists PUP280 5 518 May-05-2020, 03:47 PM Last Post: PUP280 appending list of list glennford49 2 219 Mar-29-2020, 09:33 AM Last Post: ibreeden dot product of a list by a list in a dictionary stored as a value Fraher123 1 186 Mar-25-2020, 06:08 PM Last Post: deanhystad Creating new list based on exact regex match in original list interjectdirector 1 207 Mar-08-2020, 09:30 PM Last Post: deanhystad create a list of object with a list of character studenthch 0 157 Feb-12-2020, 08:43 AM Last Post: studenthch how to compare a list to a list of lists kevthew 1 247 Dec-22-2019, 11:43 AM Last Post: ibreeden Randomly assign values in List 1 to a value in List 2 Seneca260 7 783 Dec-16-2019, 12:57 PM Last Post: BamBi25 extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 257 Dec-07-2019, 05:03 PM Last Post: ichabod801 item = index position - list of list RavCOder 9 538 Dec-02-2019, 05:24 PM Last Post: ThomasL List Comprehension - Creating a list of the length of an item help paul41 2 346 Nov-18-2019, 10:21 AM Last Post: perfringo

Forum Jump:

Users browsing this thread: 1 Guest(s)