Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 calculation with list in list
#1
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.
Quote
#2
What have you thought about, or tried?
Quote
#3
Somehow I have to use the def inside the def but I cant solve it . Or call the def repeatedly
Quote
#4
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
Quote
#5
Well I need the code to work for both lists. It has to calculate every list that you can give .
Quote
#6
(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 ?
Quote
#7
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.]
Quote
#8
Γεια σου γιαννη






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
Quote
#9
This looks like recursion
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#10
Σε ευχαριστώ το type(i) εκτός από int τι τιμες μπορεί να πάρει στην περίπτωση μας ;
Quote

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)