How to return list of lists

rusi rustompmody at gmail.com
Thu Apr 25 11:46:10 EDT 2013


On Apr 25, 8:29 pm, Wolfgang Maier <wolfgang.ma... at biologie.uni-
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]]
>>>



More information about the Python-list mailing list