I have this 4x10
(n
xm
) data matrix in csv:
1, 5, 19, 23, 7, 51, 18, 20, 35, 41
15, 34, 17, 8, 11, 93, 13, 46, 3, 10
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
First, I try to get a list of all possible sums from the first n/2
rows. With remaining last n/2
rows I do the same.
Under all possible sums of first rows I mean the following:
Example:
Row 1: 1, 2, 3
Row 2: 3, 2, 1
All possible sums list: 1 + [3, 2, 1]
; 2 + [3, 2, 1]
; 3 + [3, 2, 1]
Final list: [4, 3, 2, 5, 4, 3, 6, 5, 4]
(At the moment I do not want to remove duplicates)
For my logic I have this code:
import csv
def loadCsv(filename):
lines = csv.reader(open(filename, "rb"))
dataset = list(lines)
for i in range(len(dataset)):
dataset[i] = [float(x) for x in dataset[i]]
return dataset
data = loadCsv('btest2.txt')
divider = len(data)/2
firstPossibleSumsList = []
secondPossibleSumsList = []
#Possible sum list for the first n/2 rows:
for i in range(len(data[0])):
for j in range(len(data[0])):
firstPossibleSumsList.append(data[0][i] + data[1][j])
#Possible sum list for the last n/2 rows:
for i in range(len(data[0])):
for j in range(len(data[0])):
secondPossibleSumsList.append(data[2][i] + data[3][j])
The problem is that I divided rows manually by using data[0][i]
, data[1][i]
, data[2][i]
and so on. I want to do it more efficiently and by involving divider
variable, but I can't figure out how. In my code I depend on integers 0, 1, 2, 3
, but I wanted to split matrix rows into halves regardless of matrix dimensions.