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.
This is very easy, as strings and files are iterables too:
list()on a string turns it into a list of the individual characters; we use.strip()to remove any extra whitespace, including the newline. Thematrixfileopen file object is an iterable, so we can loop over it to process all the lines.Result: