I am very new to python and have no idea what I’m doing but I am trying to learn as I program as this is what I have done in the past with C, C++, and Cocoa.
I am writing a program with a matrix. I want to count the number of occurrences of x (x being a value in my matrix.) When I try to do this though I am not exactly sure what I am getting. A nice explanation of what I am getting and how to do things with matrices is all I am looking for. Eventually I will want to use the matrix.insert, the matrix.tofile, and the matrix.remove commands as well. Any sort of help is appreciated.
Here is my Code:
matrix = [
[2, 9, 7, 5, 8, 9, 2, 4, 6],
[8, 1, 1, 8, 4, 7, 5, 1, 3],
[5, 5, 7, 7, 9, 3, 8, 1, 5],
[1, 7, 8, 8, 2, 6, 4, 5, 9],
[3, 8, 6, 3, 1, 7, 4, 9, 6],
[9, 5, 4, 5, 9, 4, 2, 2, 3],
[1, 2, 5, 9, 7, 9, 6, 1, 1],
[7, 5, 8, 3, 2, 6, 9, 1, 5],
[3, 1, 9, 6, 7, 8, 5, 3, 4],
]
for sublist in matrix:
S = str(sublist)
print (S)
for row in matrix:
A = row.count(0)
B = row.count(1)
C = row.count(2)
D = row.count(3)
E = row.count(4)
F = row.count(5)
G = row.count(6)
H = row.count(7)
I = row.count(8)
J = row.count(9)
print (A)
for row in matrix:
if A > 0:
if B < 1:
print (B)
Here is what I get when I run the program:
[2, 9, 7, 5, 8, 9, 2, 4, 6]
[8, 1, 1, 8, 4, 7, 5, 1, 3]
[5, 5, 7, 7, 9, 3, 8, 1, 5]
[1, 7, 8, 8, 2, 6, 4, 5, 9]
[3, 8, 6, 3, 1, 7, 4, 9, 6]
[9, 5, 4, 5, 9, 4, 2, 2, 3]
[1, 2, 5, 9, 7, 9, 6, 1, 1]
[7, 5, 8, 3, 2, 6, 9, 1, 5]
[3, 1, 9, 6, 7, 8, 5, 3, 4]
0
0
0
0
0
0
0
0
0
Thank You!
You’re not exactly getting a “matrix” here, it’s really a list of lists.
What you’re getting from your
for row in matrixblock is the number of times a particular number has occurred, say9for the first time around. You’re also not printing any other value besides the occurrence of0.list.insert(i, v)will allow you to add a new valuevat a specific indexi. It’s helpful if you want to insert something ahead of or after another element in your list.There is no
list.tofile()method; you would have to write each individual line out to a file instead. You should look into file objects, and how to write to a file in the Python documentation.list.remove(v)will remove the first occurrence ofvin your list.In your last
for row in matrixblock, nothing will be printed; the variablesAthroughJare localized only to the previous loop block. You can adjust that by adding the two statements to the end of your previous loop, like so:If you’re interested in learning about lists a bit more, you should peruse the documentation, and check out a few tutorials. There are also a few free e-books available – one of which is Dive into Python 3 – which is a great resource.
EDIT: Taking another look at your code, I’ve observed that you never actually print out B. Since the number of zeroes in your “matrix” never exceed zero, you won’t be printing out B. Perhaps in your original question, you could illustrate what you want from the result a bit clearer? If you have a matrix that, in any of its rows, contains nonzero values, B will never be printed.