Python Forum
Book exercise in lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Book exercise in lists
#1
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. Huh
[[1, 2, 3, 4], [1, 2, 3], [1, 2], [1]]


[quote='sonedap' pid='70631' dateline='1549127665']
Reply
#2
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
Reply
#3
(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??
Reply
#4
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
Reply
#5
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.
Reply
#6
(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

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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
Reply
#8
(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

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
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)
Reply
#10
(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

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  py4e book exercise not working when compiled adriand 11 8,794 Jul-01-2020, 08:42 AM
Last Post: tawhidbd1248
  John Guttag Book - Finger Exercise 4 - need help to make the code better pritesh 12 10,521 May-06-2020, 05:10 PM
Last Post: riteshp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020