•
•
•
•
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
![]() |
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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!
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!
Try to generalize this approach:
python Syntax (Toggle Plain Text)
matrix3x3 = [range(1, 4), range(4, 7), range(7, 10)] print matrix3x3 # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Should you find Irony, you can keep her!
•
•
Join Date: Dec 2006
Posts: 384
Reputation:
Rep Power: 2
Solved Threads: 52
One comment first. You have 2 redundant lines in the original code 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]].
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 Last edited by woooee : Mar 17th, 2008 at 9:03 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
Other Threads in the Python Forum
- Previous Thread: dropshadow effect with PIL
- Next Thread: how to search in parent file and proplem to integrat to GUI( non english txt)PLZ help


Linear Mode