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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:23:21+00:00 2026-06-17T04:23:21+00:00

I just started to work on python and it’s a very basic question: I

  • 0

I just started to work on python and it’s a very basic question:

I have a input.txt which contains following test cases:

b---d
-d--d
--dd-
--d--
----d

Now i want to save the above content in 5 X 5 Matrix with the help of python code. So that when i want to return matrix[0][2] It returns -.

How should i do that?
I tried it but it prints wrong answer.

  • 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-17T04:23:22+00:00Added an answer on June 17, 2026 at 4:23 am

    This is very easy, as strings and files are iterables too:

    with open('input.txt') as matrixfile:
        matrix = [list(line.strip()) for line in matrixfile]
    

    list() on a string turns it into a list of the individual characters; we use .strip() to remove any extra whitespace, including the newline. The matrixfile open file object is an iterable, so we can loop over it to process all the lines.

    Result:

    >>> matrix
    [['b', '-', '-', '-', 'd'], ['-', 'd', '-', '-', 'd'], ['-', '-', 'd', 'd', '-'], ['-', '-', 'd', '-', '-'], ['-', '-', '-', '-', 'd']]
    >>> matrix[0][2]
    '-'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning python and django and I have a question. I got
I have just started work on asmall program that sends text over net (TON)
I just started to work on informix.( I have been using mysql all my
I just started working on Python and have been trying to run an outside
I have just started to look at the Scala collections library re-implementation which is
I just started programming with Python, and have some simple questions (probably). What I
I've just started to work with python, so I run into problem. I've searched
I've just started to work with python, so I run into problem. I've searched
I have just started to work on a django project( and learn django at
I have programmed in Python for a while, and just recently started using Ruby

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.