How to return list of lists

Discussion in 'Python' started by eternaltheft@gmail.com, Apr 25, 2013.

1. Guest

Hi guys, I'm having a lot of trouble with this.

My understanding of python is very basic and I was wondering how I can return this table as a list of lists.

1 2 3 4 5
3 4 5 6
5 6 7
7 8
9

The function is
def Table(n):
Then how would I make it that when the value of n is 4 it returns [[1, 2, 3], [3, 4], [5]].

Thank you

, Apr 25, 2013

2. Wolfgang MaierGuest

<eternaltheft <at> gmail.com> writes:

>
> Hi guys, I'm having a lot of trouble with this.
>
> My understanding of python is very basic and I was wondering how I can

return this table as a list of lists.
>
> 1 2 3 4 5
> 3 4 5 6
> 5 6 7
> 7 8
> 9
>
> The function is
> def Table(n):
> Then how would I make it that when the value of n is 4 it returns [[1, 2,

3], [3, 4], [5]].
>
> Thank you
>

First of all, state your question precisely!
I have no idea why with n=4 you would want to return that!
Second, the solution of this sort of problem lies in list comprehensions
combined with range(). Read up on those topics! Especially comprehensions
are so fundamental to the language that you have know about them before you
embark on any serious programming efforts.
Best,
Wolfgang

Wolfgang Maier, Apr 25, 2013

3. EternaltheftGuest

On Thursday, April 25, 2013 11:29:31 PM UTC+8, Wolfgang Maier wrote:
> <eternaltheft <at> gmail.com> writes:
>
>
>
> >

>
> > Hi guys, I'm having a lot of trouble with this.

>
> >

>
> > My understanding of python is very basic and I was wondering how I can

>
> return this table as a list of lists.
>
> >

>
> > 1 2 3 4 5

>
> > 3 4 5 6

>
> > 5 6 7

>
> > 7 8

>
> > 9

>
> >

>
> > The function is

>
> > def Table(n):

>
> > Then how would I make it that when the value of n is 4 it returns [[1, 2,

>
> 3], [3, 4], [5]].
>
> >

>
> > Thank you

>
> >

>
>
>
> First of all, state your question precisely!
>
> I have no idea why with n=4 you would want to return that!
>
> Second, the solution of this sort of problem lies in list comprehensions
>
> combined with range(). Read up on those topics! Especially comprehensions
>
> are so fundamental to the language that you have know about them before you
>
> embark on any serious programming efforts.
>
> Best,
>
> Wolfgang

Ah sorry the task is to make a function for this:
def tTable(n):
tTable(n),even n, returns the initial triangular n-1 x n-1(triangular table i posted before) table as a list of lists. For example, tTable(4) returns [[1, 2, 3], [3, 4], [5]].

Sorry if i was a bit vague, but i don't know how to explain it any better . And thanks for the tips! I will definitely read up on it when i get the time.

Eternaltheft, Apr 25, 2013
4. rusiGuest

On Apr 25, 8:29 pm, Wolfgang Maier <-
freiburg.de> wrote:
>  <eternaltheft <at> gmail.com> writes:
>
>
>
> > Hi guys, I'm having a lot of trouble with this.

>
> > My understanding of python is very basic and I was wondering how I can

>
> return this table as a list of lists.
>
>
>
> > 1  2       3       4       5
> > 3  4       5       6
> > 5  6       7
> > 7  8
> > 9

>
> > The function is
> > def Table(n):
> > Then how would I make it that when the value of n is 4 it returns [[1, 2,

> 3], [3, 4], [5]].
>
> > Thank you

>
> First of all, state your question precisely!
> I have no idea why with n=4 you would want to return that!
> Second, the solution of this sort of problem lies in list comprehensions
> combined with range(). Read up on those topics! Especially comprehensions
> are so fundamental to the language that you have know about them before you
> embark on any serious programming efforts.
> Best,
> Wolfgang

As Wolfgang says -- dunno why the 4 (my code below works for 3)
And I wonder whether you are making us do your homework.
Still heres something to start you off

def row(max,n): return range(2*n-1, max+n)
def tri(max): return [row(max,i) for i in range(1,max+1)]
>>> tri(3)

[[1, 2, 3], [3, 4], [5]]
>>>

rusi, Apr 25, 2013
5. EternaltheftGuest

Definitely not, like i said I'm having trouble doing it, you didn't have to write a code for me, i just need an explanation on how to do it. sorry if it felt like i was trying to get my homework done

Eternaltheft, Apr 25, 2013
6. Wolfgang MaierGuest

Eternaltheft <eternaltheft <at> gmail.com> writes:

>
> Ah sorry the task is to make a function for this:
> def tTable(n):
> tTable(n),even n, returns the initial triangular n-1 x n-1(triangular

table i posted before) table as a
> list of lists. For example, tTable(4) returns [[1, 2, 3], [3, 4], [5]].
>
> Sorry if i was a bit vague, but i don't know how to explain it any better

. And thanks for the tips! I will
> definitely read up on it when i get the time.
>

So if you posted this before, why are you asking again?
Could you provide a link to the post? Otherwise, I guess I'll look for it
when I get the time

Wolfgang Maier, Apr 25, 2013
7. EternaltheftGuest

Haha i'm confused? im guessing you mean the triangular table i said i posted before? its this one

1 2 3 4 5
3 4 5 6
5 6 7
7 8
9

this was the table in my original post

Eternaltheft, Apr 25, 2013
8. EternaltheftGuest

On Friday, April 26, 2013 12:05:32 AM UTC+8, Eternaltheft wrote:
> Haha i'm confused? im guessing you mean the triangular table i said i posted before? its this one
>
>
>
> 1 2 3 4 5
>
> 3 4 5 6
>
> 5 6 7
>
> 7 8
>
> 9
>
>
>
> this was the table in my original post

omg im an idiot, i didn't mention that the original table is the triangular table..

Eternaltheft, Apr 25, 2013
9. Steven D'ApranoGuest

On Thu, 25 Apr 2013 08:01:09 -0700, eternaltheft wrote:

> Hi guys, I'm having a lot of trouble with this.
>
> My understanding of python is very basic and I was wondering how I can
> return this table as a list of lists.
>
> 1 2 3 4 5
> 3 4 5 6
> 5 6 7
> 7 8
> 9

Do you know how to build lists of numbers?

a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 6]
c = [5, 6, 7]
d = [7, 8]
e = [9]

Now put them in a list:

table = [a, b, c, d, e]
print table

Got that so far? Now let's see how you might do the same thing without
typing up the inner lists by hand. For that, you want the range()
function.

a = range(1, 6) # 1 is included, 6 is excluded.
b = range(3, 7)
c = range(5, 8)
# etc.

Now let's do it without the temporary variables a, b, c, d, e.

This time, start with the outer list, only with nothing in it. Then fill
it using the range function.

table = []
table.append(range(1, 6))
table.append(range(3, 7))
# etc.

Now let's put this in a function:

def table(n):
# Returns a triangular table (list of lists).
# So far, we ignore n and hard-code the table. We'll fix this later.
table = []
table.append(range(1, 6))
table.append(range(3, 7))
# etc.
return table

You're about halfway there now. The easy half. You get to do the hard
half

You need to think about how to turn a series of repeated appends into a
loop, either a while loop or a for loop. I recommend you use a for-loop.

Start by answering these questions:

* if the argument to the function, n, is 4, how many rows and how many
columns will the table have?

* if n is 5, how many rows and columns will the table have?

* in general, for some value of n, what will be the first row?

* turn that into a call to range. Remember that if you call range with
two arguments, the first argument is included, the second is excluded:
range(5, 9) => [5, 6, 7, 8]

* what will the second row be? Hint: the *first* number in the second row
will be the first number of the first row PLUS TWO; the *last* number in
the second row will be ... what?

* turn that into a call to range. Can you see the pattern for how each
call to range will vary from the last?

* this suggests you want a for loop:

for row number 1, 2, 3, ...
table.append(make a row)

Obviously that's not real Python code. You need to write the Python code.

Write some code, and if you run into trouble, come back and ask for help.

Good luck!

--
Steven

Steven D'Aprano, Apr 25, 2013
10. EternaltheftGuest

Thank you so much for the help, I get it now . I really appreciate you taking the time to explain it into detail for me

Eternaltheft, Apr 26, 2013