Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:30:50+00:00 2026-05-27T20:30:50+00:00

I am very new to python and have no idea what I’m doing but

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:30:50+00:00Added an answer on May 27, 2026 at 8:30 pm

    You’re not exactly getting a “matrix” here, it’s really a list of lists.

    What you’re getting from your for row in matrix block is the number of times a particular number has occurred, say 9 for the first time around. You’re also not printing any other value besides the occurrence of 0.

    list.insert(i, v) will allow you to add a new value v at a specific index i. 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 of v in your list.

    In your last for row in matrix block, nothing will be printed; the variables A through J are 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:

    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)
        if A > 0:
            if B < 1:
                print (B)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to the Python world, but this seems very straight forward. Google
Very new to this, and I have no idea where to start. I want
I have very little idea what I'm doing here, I've never done anything like
I am very new to Python and I have been trying to detect missing
Im very new to C programing, but have limited experience with Python, Java, and
Hey I am very new to python but I am trying to make a
I have an idea for a web service, but am very new to web
I'm very new to Python and have a question. Currently I'm using this to
I am very new with Python and I have just received this message while
I very new to Python, and fairly new to regex. (I have no Perl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.