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

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Creating lists while n !=0


    Hi im just doing some homework and is stuck with a problem.
    I cant post the problem here, but basically i just need to create a while loop to create lists while n is not equals to 0. Im not sure as to how to do this, my first thought was:
    def myfunc(n):
    while n != 0:
    list.new() [yeah im just stuck here and its pretty stupid :P]

    I know thats completely wrong, so if anyone can give some pointers thatll be great. Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Use code tags when posting code.

    In python, the list object has no method called new():

    Code:
    >>> list.new()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: type object 'list' has no attribute 'new'
    To create a new list:

    Code:
    >>> a = list()
    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
    Mar 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by partoj
    Use code tags when posting code.

    In python, the list object has no method called new():

    Code:
    >>> list.new()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: type object 'list' has no attribute 'new'
    To create a new list:

    Code:
    >>> a = list()
    yup i realised that python doesnt have a new method now, but i need to create a "list of lists". I know if you want to create a list just use the square brackets, but im not sure how to create a "list of lists" with the condition "while n != 0". Thank you very much~
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0
    actually i think ive got it, is this right?:

    def createlists(n):
    listoflists = []
    while (n) != 0:
    listoflists.append([])
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by mrhlth
    actually i think ive got it, is this right?:

    def createlists(n):
    listoflists = []
    while (n) != 0:
    listoflists.append([])
    Close, but not quite. Did you try to run the code and see what happens? You're missing one statement to make this work.

    Here is your code with proper code tags:

    Code:
    def createlists(n):
        listoflists = []
        while (n) != 0:
            listoflists.append([])
    http://www.catb.org/esr/faqs/smart-questions.html#code
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by partoj
    Close, but not quite. Did you try to run the code and see what happens? You're missing one statement to make this work.

    Here is your code with proper code tags:

    Code:
    def createlists(n):
        listoflists = []
        while (n) != 0:
            listoflists.append([])
    ah. ran it and froze my whole pc lol. had to restart and i know what the problem is now... new code:
    Code:
    def createlists(n):
        listoflists = []
        while (n) != 0:
            listoflists.append([])
            n = n - 1
         
         return listoflists
    correct this time i hope? ran it and think its correct
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Very good, two small details:

    1. In your while statement, you do not need to have parenthesis around 'n'
    2. The expression "n = n - 1" can be expressed as "n -= 1"
    http://www.catb.org/esr/faqs/smart-questions.html#code
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    3
    How about this?

    Code:
    def listoflists(size, output=[]):
        for x in xrange(size-1): output.append([])
        return output

    edit: ohh I saw you need to use while loop...


    Code:
    def listoflists(size, output=[]):
        while len(output)!=size: output.append([])
        return output
    Last edited by eliskan; April 17th, 2013 at 03:56 AM.

IMN logo majestic logo threadwatch logo seochat tools logo