Ulaş Demirtaş
Ulaş Demirtaş

Reputation: 25

How do i sort lists lexicographically

i am doing a physics project and i want to plot something. i'm taking the mean out of every file and i want to plot those means. To read those files in, I used glob, this screwed up the whole order of my data tho. And when i try to use "sorted" it doesn't work the way. Here is my code:

#importeren van libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import glob

#definieren van de lijsten
i = 0
R1 = []
I1 = []
U1 = []
stdI = []
stdU = []

lijst = glob.glob('*.txt')
lijst = sorted(lijst)
#loop aanmaken
for i in lijst:
    try:
#        data inlezen
        data = pd.read_csv(i, skiprows=5, delimiter='\t',
                   names=['Time', 'Voltage', 'Current'], decimal=',')
    except:
        print('fout gegaan bij',i )

#   kolommen definieren  
    t = data['Time']
    U = data['Voltage']
    I = data['Current']

    Ug = np.mean(U)
    stdUg = np.std(U)
    Ig = np.mean(I)
    stdIg = np.std(I)
    R = (Ug / Ig)
    R1.append(R)
    stdI.append(stdIg)
    stdU.append(stdUg)
    I1.append(Ig)
    U1.append(Ug)


R1 = np.array(R1)
stdI = np.array(stdI)
stdU = np.array(stdU)
Irel = (stdI/Ig)
Urel = (stdU/Ug)
stdR = R*(Irel+Urel)

the way it gets sorted is:

'0A.txt',
 '10A.txt',
 '11A.txt',
 '12A.txt',
 '13A.txt',
 '14A.txt',
 '15A.txt',
 '16A.txt',
 '17A.txt',
 '18A.txt',
 '19A.txt',
 '1A.txt',
 '20A.txt',
 '21A.txt',
 '22A.txt',

instead of from 1-22

Upvotes: 1

Views: 80

Answers (1)

001
001

Reputation: 13533

The sorted function takes a few optional arguments. One of these is "key" which "specifies a function of one argument that is used to extract a comparison key from each list element". So you can use that function to convert the file name to a number and return that number. I use Regular Expressions to do that:

lijst = sorted(lijst, key=lambda x: int(re.sub("[^0-9]","", x)))

The function takes the argument x, strips all non-digits, and returns an integer. For example: "1A.txt" -> 1. This returns

['0A.txt', '1A.txt', '10A.txt', '11A.txt', '12A.txt', '13A.txt', '14A.txt', '15A.txt', '16A.txt', '17A.txt', '18A.txt', '19A.txt', '20A.txt', '21A.txt', '22A.txt']

You can use a similar technique to map each file name in the list to a number:

lijst= list(map(lambda x: int(re.sub("[^0-9]","", x)), lijst))

The return value from the map function is converted to a list. This returns

[0, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]

Upvotes: 1

Related Questions