from collections import defaultdict Input_data with open Users oleksan

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from collections import defaultdict
Input_data = []
with open('/Users/oleksandr.shubenkov/Desktop/Input.txt', 'r') as f:
for line in f:
Input_data.append([int(x) for x in line.split()])
N, M = Input_data[0]
Input_data = Input_data[1:]
data = [[0]*N for i in range(N)]
rows = defaultdict(int)
columns = defaultdict(int)
for row, column in Input_data:
rows[row]+=1
columns[column] += 1
data[row][column] -= 1
for row in rows:
data[row] = list(map(lambda x: x + rows[row], data[row]))
for column in columns:
for index in range(N):
data[index][column]+=columns[column]
with open('/Users/oleksandr.shubenkov/Desktop/Output.txt', 'a') as f:
for res_row in data:
for res_elem in res_row:
f.writelines(str(res_elem) + ' ')
f.writelines('\n')