3 回答
TA贡献1906条经验 获得超3个赞
只需在列表上应用排序():”
filename_last_lines = [[(filename, subprocess.check_output(['tail', '-1', path +
filename]))] for filename in [f for f in sorted(listdir(path)) if isfile(join(path, f)) and
f.endswith('.txt')]]
TA贡献1841条经验 获得超3个赞
也许你只需要对文件名进行排序:
filename_last_lines = [[(filename, subprocess.check_output(['tail', '-1', path +
filename]))] for filename in sorted([f for f in listdir(path) if isfile(join(path, f)) and
f.endswith('.txt')])]
TA贡献1831条经验 获得超4个赞
您将必须使用自定义函数的列表。sortkey
filename_last_lines.sort(key=lambda x: int(x[0][6:-4]))
让我们解码 lambda 的作用。
列表的每个元素都是一个元组,因此抓取元组的第一个元素,即文件名。x[0]
然后,我们要从文件名中提取数字作为整数,因此:
In [1]: x = ('coord_70.txt', 1)
Out[1]: ('coord_70.txt', 1)
In [2]: int(x[0][6:-4])
Out[2]: 70
添加回答
举报
