Python Forum
Book exercise in lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Book exercise in lists (/thread-15838.html)

Pages: 1 2


Book exercise in lists - sonedap - Feb-02-2019

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']


RE: Book exercise in lists - ichabod801 - Feb-02-2019

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).


RE: Book exercise in lists - sonedap - Feb-02-2019

(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??


RE: Book exercise in lists - ichabod801 - Feb-02-2019

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.


RE: Book exercise in lists - sonedap - Feb-02-2019

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.


RE: Book exercise in lists - perfringo - Feb-02-2019

(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.


RE: Book exercise in lists - sonedap - Feb-02-2019

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


RE: Book exercise in lists - perfringo - Feb-02-2019

(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



RE: Book exercise in lists - sonedap - Feb-02-2019

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)


RE: Book exercise in lists - perfringo - Feb-02-2019

(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!.