[英]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. 我确实想读取一些.csv文件并打印第5列的最小值和最大值。 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: 我的.csv文件的命名如下:
0.csv, 1.csv ... 10.csv ...2463.csv 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. 看到我的argmin没有显示正确的文件后,我意识到fn没有排序。 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: 我认为需要使用将文件名转换为
integer
s sorted
:
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.