Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Book exercise in lists sonedap Silly Frenchman Posts: 30 Threads: 4 Joined: Jan 2019 Reputation: 0 Likes received: 0 #1 Feb-02-2019, 06:19 PM (This post was last modified: Feb-02-2019, 06:19 PM by sonedap. Edited 5 times in total.) My university book has lots of exercises to practice and i found this one which wants to make a list with lists and then find sum of each list. As example it gives that the result should be something like this: ex1=[[1],[1,2],[1,2,3],[1.2.3.4]] so when i am trying to make this list with this code ```ex1=[] for i in range (5): ex1.append(range(1,5-i)) print(ex1)```i get this result: [[1, 2, 3, 4], [1, 2, 3], [1, 2], [1], []] which is reversed of what the book wants and also i get an empty []. why is that happening? i found why there is the empty [] and i change the code into this: ```ex1=[] for i in range (4): ex1.append(range(1,5-i)) print(ex1)```but i still get the reverse result. [[1, 2, 3, 4], [1, 2, 3], [1, 2], [1]] [quote='sonedap' pid='70631' dateline='1549127665'] ichabod801 Bunny Rabbit Posts: 3,513 Threads: 73 Joined: Sep 2016 Reputation: 194 Likes received: 1069 #2 Feb-02-2019, 06:58 PM Because you are using 5-i. range(5) gives 0, 1, 2, 3, 4, in that order. Five minus that is 5, 4, 3, 2, 1. So your list is [range(1, 5), range(1, 4), range(1, 3), range(1, 2), range(1, 1)]. The empty list is coming from range(1, 1). Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures sonedap Silly Frenchman Posts: 30 Threads: 4 Joined: Jan 2019 Reputation: 0 Likes received: 0 #3 Feb-02-2019, 07:51 PM (Feb-02-2019, 06:58 PM)ichabod801 Wrote: Because you are using 5-i. range(5) gives 0, 1, 2, 3, 4, in that order. Five minus that is 5, 4, 3, 2, 1. So your list is [range(1, 5), range(1, 4), range(1, 3), range(1, 2), range(1, 1)]. The empty list is coming from range(1, 1). How can i change it so the printing isn't [range(1,5)] etc but the numbers? Query:Why simple exercises are so difgicult in their solution?? ichabod801 Bunny Rabbit Posts: 3,513 Threads: 73 Joined: Sep 2016 Reputation: 194 Likes received: 1069 #4 Feb-02-2019, 09:15 PM I was just using range(1, 5) to try to clear about what was underlying the loop output you are getting. In Python 2.7, range(1, 5) will return [1, 2, 3, 4]. In Python 3.0+ it will return a range object, which maybe a bit confusing at first. You can get the list in Python 3.0+ with list(range(1, 5)). You should be using the latest version of Python. Support for 2.7 will end in 11 months. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures sonedap Silly Frenchman Posts: 30 Threads: 4 Joined: Jan 2019 Reputation: 0 Likes received: 0 #5 Feb-02-2019, 10:08 PM I am using 3.7 and trying the code again i am taking as output [range(1,5] etc.. So,since the output is range that means the code is wrong.? And now i am wondering why i am trying to learn python since i am not good. perfringo Verb Conjugator Posts: 720 Threads: 1 Joined: Jun 2018 Reputation: 64 Likes received: 143 #6 Feb-02-2019, 10:15 PM (This post was last modified: Feb-02-2019, 10:24 PM by perfringo.) (Feb-02-2019, 07:51 PM)sonedap Wrote: How can i change it so the printing isn't [range(1,5)] etc but the numbers? Query:Why simple exercises are so difgicult in their solution?? Solution is not difficult. You just need to articulate what you want to do. Can you say in spoken language what your code should do? Unambiguously? So that other person can understand? You are in right track, but your range is not correct. You want growing range, so you should use i+1 as end of range (range(1, 2) --> 1, range(1, 3) --> 1, 2 etc. One way to achieve desired results using list comprehension: ```>>> [list(range(1, i+1)) for i in range(1, 5)] [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]] ```Another way is to correct your code: ```>>> result = list() >>> for i in range(1, 5): ... result.append(list(range(1, i+1))) ... >>> result [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]] ```I just mention that this is Python 3. (Feb-02-2019, 10:08 PM)sonedap Wrote: And now i am wondering why i am trying to learn python since i am not good. Don't give up so easily. It takes time. I suggest you to read this: Teach Yourself Programming in Ten Years. You probably don't know who Peter Norvig dude is, so you can read about him in Wikipedia. I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Life of Brian: Conjugate the verb, "to go" ! sonedap Silly Frenchman Posts: 30 Threads: 4 Joined: Jan 2019 Reputation: 0 Likes received: 0 #7 Feb-02-2019, 10:24 PM I want to make a list which contains other lists in the following pattern: List=[[1],[1,2],[1,2,3]] etc the second part is to find sum of each sublist and print a message like "The first elment of the list is [1] with sum 1" "The second element of the list is [1,2] with sum 3" Etc Thats what this exercise is about perfringo Verb Conjugator Posts: 720 Threads: 1 Joined: Jun 2018 Reputation: 64 Likes received: 143 #8 Feb-02-2019, 10:50 PM (This post was last modified: Feb-02-2019, 10:51 PM by perfringo. Edited 2 times in total.) (Feb-02-2019, 10:24 PM)sonedap Wrote: I want to make a list which contains other lists in the following pattern: List=[[1],[1,2],[1,2,3]] etc the second part is to find sum of each sublist and print a message like "The first elment of the list is [1] with sum 1" "The second element of the list is [1,2] with sum 3" Etc Thats what this exercise is about You have a code how to build required list. Now you have to sum sublist elements. Fortunately in Python there is function sum() Easiest way to get sum is add sum to previous solution and skip converting to list: ```result = list() >>> for i in range(1, 5): ... result.append(sum(range(1, i+1))) ... >>> result [1, 3, 6, 10] ```If you think little about this code you probably see, that instead appending to list you can print them out and do that in one loop: ```>>> for i in range(1, 5): ... print(f'The first element of the list is {list(range(1, i+1))} with sum {sum(range(1, i+i))}') ... The first element of the list is [1]with sum 1 The first element of the list is [1, 2]with sum 3 The first element of the list is [1, 2, 3]with sum 6 The first element of the list is [1, 2, 3, 4]with sum 10 ```Would you call these two rows of code complicated solution? I noticed, that there is 'The first' element. It can be easily remedied by adding zip to the code: ```>>> for i, v in zip(range(1, 5), ['first', 'second', 'third', 'fourth']): ... print(f'The {v} element of the list is {list(range(1, i+1))} with sum {sum(range(1, i+i))}') ... The first element of the list is [1] with sum 1 The second element of the list is [1, 2] with sum 6 The third element of the list is [1, 2, 3] with sum 15 The fourth element of the list is [1, 2, 3, 4] with sum 28 ``` I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Life of Brian: Conjugate the verb, "to go" ! sonedap Silly Frenchman Posts: 30 Threads: 4 Joined: Jan 2019 Reputation: 0 Likes received: 0 #9 Feb-02-2019, 10:58 PM Well,since I am new to python I think it's better to try solve the exercise with everything I have read so far. The zip part and f' I haven't found them in the books yet so I am trying to solve it with using for or while.. The simplest way possible at the moment! (if I succeed I will be happiest person in planet Earth) perfringo Verb Conjugator Posts: 720 Threads: 1 Joined: Jun 2018 Reputation: 64 Likes received: 143 #10 Feb-02-2019, 11:32 PM (This post was last modified: Feb-02-2019, 11:32 PM by perfringo. Edited 2 times in total.) (Feb-02-2019, 10:58 PM)sonedap Wrote: The zip part and f' I haven't found them in the books yet so I am trying to solve it with using for or while.. The simplest way possible at the moment! (if I succeed I will be happiest person in planet Earth) zip() is Python built-in function and you can read about in Python documentation. f-string is available in Python 3.6 or newer. You can read about them in PEP 498 -- Literal String Interpolation . Don't limit yourself to 'the book'. Be curious and try to learn and understand whether it is in book or not. If you want to limit yourself only to for-loop then you can to something like that: ```>>> sequentials = ['first', 'second', 'third', 'fourth'] >>> for i in range(1, 5): ... print('The {} element of the list is {} with sum {}'.format(sequentials[i-1], list(range(1, i+1)), sum(range(1, i+1)))) The first element of the list is [1] with sum 1 The second element of the list is [1, 2] with sum 3 The third element of the list is [1, 2, 3] with sum 6 The fourth element of the list is [1, 2, 3, 4] with sum 10 ```This code uses .format method which is somewhat 'older' nowadays, one resource to get acquainted with this string metohd is Using % and .format() for great good!. I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Life of Brian: Conjugate the verb, "to go" ! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Help in exercise for loop kais92 13 848 Dec-01-2018, 05:16 PM Last Post: kais92 Python exercise janaraguz 2 451 Sep-22-2018, 08:43 PM Last Post: ichabod801 py4e book exercise not working when compiled adriand 10 1,581 Jun-13-2018, 01:01 PM Last Post: adriand Help with Dictionaries and Lists of Lists Nate5800 5 855 Apr-23-2018, 06:42 PM Last Post: Nate5800 Help with test exercise lm777 3 917 Feb-21-2018, 10:42 PM Last Post: nilamo John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 9 2,121 Feb-13-2018, 10:31 PM Last Post: pritesh This is an open book home assignment but i really need your help Samiha 3 1,372 Oct-04-2017, 05:39 PM Last Post: nilamo Simple exercise - how to write in python tomtom55 6 1,779 Sep-28-2017, 07:18 PM Last Post: nilamo Help with an exercise Cerobug 8 1,294 Sep-20-2017, 09:10 PM Last Post: nilamo The Python Book altered rock paper scissors Python_Noob 0 955 Sep-18-2017, 06:13 AM Last Post: Python_Noob

Forum Jump:

Users browsing this thread: 1 Guest(s)