Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import random
- def input_matrix(file=sys.stdin):
- n, m = map(int, file.readline().split())
- matrix = [list(map(float, file.readline().split())) for _ in range(n)]
- assert all(len(row) == m for row in matrix), "row length != m"
- return matrix
- def print_matrix(matrix, file=sys.stdout):
- n, m = len(matrix), len(matrix[0])
- assert all(len(row) == m for row in matrix), "inconsistent row length"
- print(n, m, file=file)
- for row in matrix:
- print(*row, file=file)
- with open("matrix.txt", "w") as file:
- matrix = [[random.randrange(10) for _ in range(3)] for _ in range(5)]
- print_matrix(matrix, file=file) # вывод в файл
- with open("matrix.txt", "r") as file:
- matrix = input_matrix(file=file) # чтение из файла
- print_matrix(matrix) # вывод на экран
Advertisement
Add Comment
Please, Sign In to add comment