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
  List Comprehension - Creating a list of the length of an item help paul41 2 136 Nov-18-2019, 10:21 AM
Last Post: perfringo
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 150 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 131 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Creating a list from a comprehension using a list rm197 6 149 Sep-30-2019, 07:02 PM
Last Post: rm197
  Converting List into list of tuples ARV 4 327 Sep-28-2019, 04:58 AM
Last Post: perfringo
  convert list of dict to dict of array of list with pop up kk230689 1 145 Sep-13-2019, 07:29 AM
Last Post: perfringo
  removing quotes from a list and keep type list evilcode1 3 229 Aug-03-2019, 11:07 AM
Last Post: perfringo
  Changing elements of a list to match another list rockproper 2 214 Jul-11-2019, 02:13 PM
Last Post: ichabod801
  Create new list from another list based on condition khalidreemy 1 296 Jun-11-2019, 10:21 AM
Last Post: buran
  Iterating list of oredereddict for creating new list of ordereddict babypython 7 407 May-05-2019, 05:32 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)