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 8537145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:49:39+00:00 2026-06-11T10:49:39+00:00

How do you compare a matrix row (ie list of lists) with a given

  • 0

How do you compare a matrix row (ie list of lists) with a given string?

index = 99999
for i in range(len(text)):
    if (matrix[i][0:len(text)] == text):
        index = i

I want “index” to be the number of row for which “row == text”, but the above code outputs 99999.

I know for sure that exactly one of the rows contains the string.
For example, the matrix is

 ['a', 'i', 'n', 'e', 'm']
 ['e', 'm', 'a', 'i', 'n']
 ['i', 'n', 'e', 'm', 'a']
 ['m', 'a', 'i', 'n', 'e']
 ['n', 'e', 'm', 'a', 'i']

and I want to know which row is “maine” (number 3 in this case).
Thanks!

  • 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-06-11T10:49:41+00:00Added an answer on June 11, 2026 at 10:49 am

    Try

    try:
        index = matrix.index(list(text))
    except IndexError:
        index = 99999
    

    list(text) turns the string into a list of characters. list.index searches for the item you specify (using == as the equality comparison) and returns its index if found, or raises IndexError if not found.

    I’d also not recommend using 99999 as the ‘not found’ value; it’s rather safer to use a value like -1 or (better yet) just leave the exception alone unless you intend to handle it.

    If you know the string must be in the matrix, then index = matrix.index(list(text)) is all you need.

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

Sidebar

Related Questions

I have a function that returns a comparison matrix from a given list: def
I have made a List of Lists implementation of a sparse matrix, and I've
I'd like to build a function, which, given a 2D matrix and some element
I would like to use a match with function to compare two matrix. I
My goal is to load values in a 5x5 matrix and compare each value
I have this simple graph: name -> string ^ | v label let matrix
I have created a 2D matrix like this: 45 67 Row Fine 25 22
I am trying to compare a string to a string value. Seems rather simple,
I need to compare 3 216x216 matrix (data correlation matrix, events etc) . can
I am doing colSums(x) of a matrix x in R and to compare results

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.