简体   繁体   中英

Sort list of Strings by numbers in python

I do want to read some .csv files and print the minimum and maximum value of the 5th column. I also want to print in which file I can find the max or min.

My code is right now:

import pandas, glob
import numpy as np

path = "/home/path/to/log/"

fn = glob.glob(path + "*.csv") 
list_of_dfs = [pandas.read_csv(filename, header=None) for filename in fn]
k = len(list_of_dfs)
b = np.zeros((k, 1))
cnt = 0 
for i in list_of_dfs:
    b[cnt,0] = np.min(i[4])
    cnt = cnt + 1
print(np.min(b[:,0]))
print(np.argmin(b[:,0]))

My .csv Files are named like:

0.csv, 1.csv ... 10.csv ...2463.csv

After seeing that my argmin doesn't show the correct file I realised that fn isn't sorted. I found this Sort filenames in directory in ascending order , where the solution was:

fn.sort(key=lambda f: int(filter(str.isdigit, f)))

But with this line I get the following error:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'filter'

Any Suggestions?

I think need sorted with convert file names to integer s:

fn = ['0.csv', '1.csv', '2463.csv', '10.csv']

fn = sorted(fn, key=lambda f: int(f.split('.')[0]))
print (fn)
['0.csv', '1.csv', '10.csv', '2463.csv']

If there are full paths:

print (fn)
['files\\1.csv', 'files\\10.csv', 'files\\2.csv']

fn = sorted(fn, key=lambda f: int(os.path.basename(f).split('.')[0]))
print (fn)
['files\\1.csv', 'files\\2.csv', 'files\\10.csv']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM