'How to change from reading files to sys.stdin.readlines()
Here is my original code.
f = open("p3.in","r")
while True:
n = f.readline()
print(n)
if n == 0:
break
Map = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
l = f.readline()
for j in list(map(int, l.split())):
Map[i][j] = 1
for i in range(n):
for j in range(n):
print(Map[i][j], end=" ")
print()
This is what I used for p3.in.
3
1
2 0
1
4
1 3 2
2 3
3
0 2
0
I can print with the original code,
3
0 1 0
1 0 1
0 1 0
4
0 1 1 1
0 0 1 1
0 0 0 1
1 0 1 0
0
but I want to change it to
import sys
while True:
n = sys.stdin.readlines()
Because my file submission system already has this file of p3.in loaded in the system. I am unable to print the results after modifying:
TypeError: 'list' object cannot be interpreted as an integer Map = [[0 for i in range(n)] for j in range(n)]
But I have no clue how to change the code after several attempts. Please help me with the problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|