#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    How to return list of lists


    Hi

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by Bobdy
    Hi

    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
    Are you in the same class as "mrhlth" ?

    http://forums.devshed.com/python-programming-11/listoflists-944040.html
    http://www.catb.org/esr/faqs/smart-questions.html#code
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by partoj
    Are you in the same class as "mrhlth" ?
    yeah probably lol, i've read that thread but still dont have a clue what to do
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    In that case, just reuse the mrhlth function then delete all the zeros.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    the mrhlth function?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,696
    Rep Power
    480
    Use the relevant function in the other thread.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Use the relevant function in the other thread.
    do you mean this one?

    Code:
    def f(n):
        '''
            whacko array creator
            >>> f(4) == [[1,2,3],[0,3,4],[0,0,5]]
            True
        '''
        A = [[0 for i in range(n - 1)] for j in range(n - 1)]
        A[0][:] = range(1, n) # first row  ******1
        for i in range(1, n - 1): # (yes, this range would need to change as well to answer the second question)
            for j in range(i, n):
                A[i][j] = A[i-1][j] + 1  # ******2
        return A
    I tried using this one but I keep getting 0s returned :s

IMN logo majestic logo threadwatch logo seochat tools logo