| | |
Help with Appending Number Matrix
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
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: 1,045
Reputation:
Solved Threads: 294
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]].
Python Syntax (Toggle Plain Text)
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 10:03 pm.
![]() |
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
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog decimals dictionaries dictionary directory drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






