Array is specified

A1,1 A1,2 A1,3 A1,4 A1,5 A1,6 ... A1,n 

Need to withdraw

 A1,2-A1,1 A1,3-A1,1 A1,3-A1,2 A1,4-A1,1 A1,4-A1,2 A1,4-A1,3 A1,5-A1,1 A1,5-A1,2 A1,5-A1,3 A1,5-A1,4 A1,6-A1,1 A1,6-A1,2 A1,6-A1,3 A1,6-A1,4 A1,6-A1,5 ... ... ... ... ... ... A1,n-A1,1 A1,n-A1,2 A1,n-A1,3 A1,n-A1,4 A1,n-A1,5 A1,n-A1,n-1 

My algorithm

  ls = list(range(1, 6)) res = [] for i, item in enumerate(ls): buf = [item1 - item for item1 in ls[i+1:]] res.append(buf) for s in range(len(ls)-1): print(res[s]) 

I get

Displays data in a horizontal scan.

  [1, 2, 3, 4] [1, 2, 3] [1, 2] [1] 

instead of vertical

  [1] [2] [1] [3] [2] [1] [4] [3] [2] [1] 

but it's not scary when working with arrays, so I go further

Instead

  ls = list(range(1, 6)) 

which was given to test the health of the cycle I fill in the data from the file

In this case, I import the values ​​as str

  with open('Test3.csv') as f: ls = f.read() res = [] for i, item in enumerate(ls): buf = [item1 - item for item1 in ls[i+1:]] res.append(buf) for s in range(len(ls)-1): print(res[s]) 

Also tried so

In this case, I import the values ​​as list

  ls = open('Test3.csv').readlines() res = [] for i, item in enumerate(ls): buf = [item1 - item for item1 in ls[i+1:]] res.append(buf) for s in range(len(ls)-1): print(res[s]) 

The same error is issued unsupported operand type (s) for -: 'str' and 'str'

As I understand this error, for the operation of the loop (for ...), integer values ​​are needed so that an operation inside the loop can be performed.

I can only understand how to mitigate this error and all

Uraa happened

  ls = open('Test3.csv').readlines() res = [] for i, item in enumerate(ls): buf = [int(item1) - int(item) for item1 in ls[i+1:]] res.append(buf) for s in range(len(ls)-1): print(res[s]) 
  • one
    these are numerical values ​​... an element of the matrix A1,1 is a certain numerical value, etc. - UnoMan

1 answer 1

If I correctly understood the condition of the question:

 In [1]: import pandas as pd ...: import numpy as np ...: In [2]: a = pd.Series(np.arange(1,6)) In [3]: print(a) 0 1 1 2 2 3 3 4 4 5 dtype: int32 In [4]: df = pd.DataFrame(index=np.arange(len(a))) ...: In [5]: for i in range(len(a)): ...: df[i] = a.shift(i) - a.iloc[i] ...: In [6]: print(df) 0 1 2 3 4 0 0 NaN NaN NaN NaN 1 1 -1.0 NaN NaN NaN 2 2 0.0 -2.0 NaN NaN 3 3 1.0 -1.0 -3.0 NaN 4 4 2.0 0.0 -2.0 -4.0 
  • Column 0 counted correctly. - UnoMan
  • $ MaxU Column 0 counted correctly. Column 1-4 is not correct. Column 0 is calculated as follows: (2-1 = 1), (3-1 = 2), (4-1 = 3), (5-1 = 4). In column 1, the correct calculations begin with the intersection of line 3 and column 1, i.e. They are shifted down by 2 lines. In column 2, the calculations are shifted down by 3 lines, etc. - UnoMan
  • @UnoMan, give the expected output for the input array in question: [1,2,3,4] - MaxU
  • gave an example of the expected output - UnoMan
  • $ MaxU example is given in the question text - UnoMan