User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 392,045 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,283 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 342 | Replies: 2
Reply
Join Date: Mar 2008
Posts: 1
Reputation: okcomputer24 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
okcomputer24 okcomputer24 is offline Offline
Newbie Poster

Help Help with Appending Number Matrix

  #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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Location: France
Posts: 965
Reputation: bumsfeld is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 42
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Posting Shark

Re: Help with Appending Number Matrix

  #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  
Join Date: Dec 2006
Posts: 384
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 52
woooee woooee is offline Offline
Posting Whiz

Re: Help with Appending Number Matrix

  #3  
Mar 17th, 2008
One comment first. You have 2 redundant lines in the original code
n = int(input("Give me an integer: "))
list1 = []
##i = 1     ## this line is redundant-the for() loop handles initializing i
for i in range(1,n+1):
     print "i =", i     ## this will show what the for() loop is doing
     list1 = list1 + [i]
     #i = i + 1     ## this line is redundant-the for() loop also handles incrementing
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 9:03 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 11:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC