Help with Appending Number Matrix

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 1
Reputation: okcomputer24 is an unknown quantity at this point 
Solved Threads: 0
okcomputer24 okcomputer24 is offline Offline
Newbie Poster

Help with Appending Number Matrix

 
0
  #1
Mar 17th, 2008
I am wanting to produce a number matrix which will result in displaying the numbers in chronological order. For example, if a 3 x 3 matrix is desired, the output would result as such: [[1,2,3],[4,5,6],[7,8,9]]

The first part of my code can produce the initial list. However I am unsure how to append the lists that would continue this matrix.

n = int(input("Give me an integer: "))
list1 = []
i = 1
for i in range(1,n+1):
list1 = list1 + [i]
i = i + 1
print list1

Any help or advice would be greatly appreciated. Thank you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Help with Appending Number Matrix

 
0
  #2
Mar 17th, 2008
Try to generalize this approach:
  1. matrix3x3 = [range(1, 4), range(4, 7), range(7, 10)]
  2. print matrix3x3 # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,045
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 294
woooee woooee is offline Offline
Veteran Poster

Re: Help with Appending Number Matrix

 
0
  #3
Mar 17th, 2008
One comment first. You have 2 redundant lines in the original code
  1. n = int(input("Give me an integer: "))
  2. list1 = []
  3. ##i = 1 ## this line is redundant-the for() loop handles initializing i
  4. for i in range(1,n+1):
  5. print "i =", i ## this will show what the for() loop is doing
  6. list1 = list1 + [i]
  7. #i = i + 1 ## this line is redundant-the for() loop also handles incrementing
  8. print list1
To do what I think you want, you have to ask the user for the maximum number to count to, and the "grouping number" or whatever you want to call how many numbers to put in one group. You then use a second, nested for loop underneath the one you have to put the "grouping number" of numbers in a list which you append to the original once the inner loop has finished. Hence a list of lists [[1,2,3], [4,5,6]].
Last edited by woooee; Mar 17th, 2008 at 10:03 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC