There is a list, for example:

["2 -1 5 4"] 

These are the coordinates of the points of the vector, x1, y1, x2, y2, respectively. It is necessary to find the length of the vector formed by these points.

Prompt more "Python" way to solve this problem. How to properly pull values ​​from this line, then to calculate everything?

The fact that the list is just one line, do not pay attention, it should be so. :)

  • I understand correctly - do you have coordinates of strictly two points on a plane given in the form of a list consisting of one line? - MaxU
  • File is given. Each line contains 4 numbers - these are the coordinates of the points. Several lines. I read the file using readlines () and get a list of lines, clear it from "\ n" and get a clean list with lines in which these coordinates are recorded. - Antoxer
  • Can you give an example file in question (say, 3-5 lines)? - MaxU
  • Well, the example is very simple) - Antoxer
  • one
    intlist = [int(x) for x in strlist[i].split()] - MBo

2 answers 2

Solution using the Pandas module:

 import pandas as pd # pip install pandas # читаем файл в pandas.DataFrame df = pd.read_csv(filename, delim_whitespace=True, header=None, names=['x1','y1','x2','y2']) 

got:

 In [373]: df Out[373]: x1 y1 x2 y2 0 2 -1 5 4 1 1 2 3 4 2 10 3 18 1 

count the distance between points for each line:

 df = df.eval("dist = ((x1-x2)**2 + (y1-y2)**2)**0.5") 

result:

 In [375]: df Out[375]: x1 y1 x2 y2 dist 0 2 -1 5 4 5.830952 1 1 2 3 4 2.828427 2 10 3 18 1 8.246211 

    You can do it using regular expressions:

     import re map(int, re.findall(r'\d+|-\d+', l[0])) # вернёт [2, -1, 5, 4]