Search code examples
pythonpython-3.xline

python sorting lines in a file beginning with numbers in the right order


I have a file which looks like:

11 -7.072985E-17  5.570644E-18  2.763979E-17
12 -6.420375E-17 -5.456010E-19  2.229937E-17
9 -8.360102E-17  1.172390E-17  2.492073E-17
10 -1.046834E-16  1.106907E-17  1.840354E-17
17  2.000000E-01  1.172390E-17  2.492073E-17
18  2.000000E-01  1.106907E-17  1.840354E-17
19  2.000000E-01  5.570644E-18  2.763979E-17
20  2.000000E-01 -5.456010E-19  2.229937E-17
2  0.000000E+00  0.000000E+00  0.000000E+00
6  2.000000E-01  0.000000E+00  0.000000E+00
4  0.000000E+00  0.000000E+00  0.000000E+00
1  0.000000E+00  0.000000E+00  0.000000E+00
13 -8.360102E-17  1.172390E-17  2.492073E-17
14 -1.046834E-16  1.106907E-17  1.840354E-17
15 -7.072985E-17  5.570644E-18  2.763979E-17
16 -6.420375E-17 -5.456010E-19  2.229937E-17
3 -0.000000E+00 -0.000000E+00 -0.000000E+00
5  2.000000E-01 -0.000000E+00 -0.000000E+00
7  2.000000E-01 -0.000000E+00 -0.000000E+00
8  2.000000E-01 -0.000000E+00 -0.000000E+00
21  2.000000E-01  1.172390E-17  2.492073E-17
22  2.000000E-01  1.106907E-17  1.840354E-17

how can i order the lines so that it will change the order of it resulting in lines starting with 1,2,.... looking like:

1  0.000000E+00  0.000000E+00  0.000000E+00
2  0.000000E+00  0.000000E+00  0.000000E+00
.....
21  2.000000E-01  1.172390E-17  2.492073E-17


Solution

  • The reason I asked for the file type is that the code depends a bit on how you read it. But if I just take your data as given above and input as a list of lists, then the below code will sort as you ask.

    import pandas as pd
    dat = [['11', '-7.072985E-17', '5.570644E-18', '2.763979E-17'],
           ['12', '-6.420375E-17', '-5.456010E-19', '2.229937E-17'],
           ['9', '-8.360102E-17', '1.172390E-17', '2.492073E-17'],
           ['10', '-1.046834E-16', '1.106907E-17', '1.840354E-17'],
           ['17', '2.000000E-01', '1.172390E-17', '2.492073E-17'],
           ['18', '2.000000E-01', '1.106907E-17', '1.840354E-17'],
           ['19', '2.000000E-01', '5.570644E-18', '2.763979E-17'],
           ['20', '2.000000E-01', '-5.456010E-19', '2.229937E-17'],
           ['2', '0.000000E+00', '0.000000E+00', '0.000000E+00'],
           ['6', '2.000000E-01', '0.000000E+00', '0.000000E+00'],
           ['4', '0.000000E+00', '0.000000E+00', '0.000000E+00'],
           ['1', '0.000000E+00', '0.000000E+00', '0.000000E+00'],
           ['13', '-8.360102E-17', '1.172390E-17', '2.492073E-17'],
           ['14', '-1.046834E-16', '1.106907E-17', '1.840354E-17'],
           ['15', '-7.072985E-17', '5.570644E-18', '2.763979E-17'],
           ['16', '-6.420375E-17', '-5.456010E-19', '2.229937E-17'],
           ['3', '-0.000000E+00', '-0.000000E+00', '-0.000000E+00'],
           ['5', '2.000000E-01', '-0.000000E+00', '-0.000000E+00'],
           ['7', '2.000000E-01', '-0.000000E+00', '-0.000000E+00'],
           ['8', '2.000000E-01', '-0.000000E+00', '-0.000000E+00'],
           ['21', '2.000000E-01', '1.172390E-17', '2.492073E-17'],
           ['22', '2.000000E-01', '1.106907E-17', '1.840354E-17']]
    
    df = pd.DataFrame(dat)
    df[0] = df[0].astype(int)
    df = df.sort_values(by=0)
    print(df)