如何使用 Python 对包含子文件夹的文件夹中的文件名进行递归排序
How do I sort filenames in a folder containing sub-folders recursively using Python
我是 Python 使用 Databricks 的新手。我有一个包含多个子文件夹的文件夹,其中包含文件名如下所示的文件:
Input (A_B)
=====
1_1.json.gz
1_22.json.gz
7_33.json.gz
1_4.json.gz
2_1.json.gz
2_22.json.gz
9_33.json.gz
2_4.json.gz
如何从文件名中提取两个整数并使用它们以这种方式进行排序:
Output (Order by B Asc, A Desc)
======
2_1.json.gz
1_1.json.gz
2_4.json.gz
1_4.json.gz
2_22.json.gz
1_22.json.gz
9_33.json.gz
7_33.json.gz
我尝试了以下代码,但无法获得预期的输出:
import os
from os import path
path = '/dbfs/FileStore/MainFolder'
arrayfiles = []
for root,dirs,files in os.walk(path):
for file in files:
if file.endswith('.json.gz'):
print(file)
arrayfiles.append(file)
print(arrayfiles)
arrayfiles.sort()
print(arrayfiles)
如果你也能帮我解决这个问题,我又遇到了类似的问题,上面输入中的 great.So 让我们将下划线前的整数视为 'A',下划线后的整数视为 [=23] =] 并且你帮助我的输出是按(B Asc,A Desc)排序的。我如何按 B,A 或 B Desc 和 A Ascn 排序?我可以使用相同的冒泡排序算法吗? –
如果我有字符串而不是整数怎么办......那我该如何排序??
试试这个冒泡排序算法:
def custom_bubble_sort(arr):
def swap(i, j):
arr[i], arr[j] = arr[j], arr[i]
def getFirstNum(st):
return int(st.split(".")[0].split("_")[0])
def getSecondNum(st):
return int(st.split(".")[0].split("_")[1])
n = len(arr)
swapped = True
x = -1
while swapped:
swapped = False
x = x + 1
for i in range(1, n-x):
if getSecondNum(arr[i - 1]) > getSecondNum(arr[i]):
swap(i - 1, i)
swapped = True
elif getSecondNum(arr[i - 1]) == getSecondNum(arr[i]):
if getFirstNum(arr[i - 1]) < getFirstNum(arr[i]):
swap(i - 1, i)
swapped = True
return arr
a = ['1_1.json.gz',
'1_22.json.gz',
'7_33.json.gz',
'1_4.json.gz',
'2_1.json.gz',
'2_22.json.gz',
'9_33.json.gz',
'2_4.json.gz']
print(custom_bubble_sort(a))
我是 Python 使用 Databricks 的新手。我有一个包含多个子文件夹的文件夹,其中包含文件名如下所示的文件:
Input (A_B)
=====
1_1.json.gz
1_22.json.gz
7_33.json.gz
1_4.json.gz
2_1.json.gz
2_22.json.gz
9_33.json.gz
2_4.json.gz
如何从文件名中提取两个整数并使用它们以这种方式进行排序:
Output (Order by B Asc, A Desc)
======
2_1.json.gz
1_1.json.gz
2_4.json.gz
1_4.json.gz
2_22.json.gz
1_22.json.gz
9_33.json.gz
7_33.json.gz
我尝试了以下代码,但无法获得预期的输出:
import os
from os import path
path = '/dbfs/FileStore/MainFolder'
arrayfiles = []
for root,dirs,files in os.walk(path):
for file in files:
if file.endswith('.json.gz'):
print(file)
arrayfiles.append(file)
print(arrayfiles)
arrayfiles.sort()
print(arrayfiles)
如果你也能帮我解决这个问题,我又遇到了类似的问题,上面输入中的 great.So 让我们将下划线前的整数视为 'A',下划线后的整数视为 [=23] =] 并且你帮助我的输出是按(B Asc,A Desc)排序的。我如何按 B,A 或 B Desc 和 A Ascn 排序?我可以使用相同的冒泡排序算法吗? – 如果我有字符串而不是整数怎么办......那我该如何排序??
试试这个冒泡排序算法:
def custom_bubble_sort(arr):
def swap(i, j):
arr[i], arr[j] = arr[j], arr[i]
def getFirstNum(st):
return int(st.split(".")[0].split("_")[0])
def getSecondNum(st):
return int(st.split(".")[0].split("_")[1])
n = len(arr)
swapped = True
x = -1
while swapped:
swapped = False
x = x + 1
for i in range(1, n-x):
if getSecondNum(arr[i - 1]) > getSecondNum(arr[i]):
swap(i - 1, i)
swapped = True
elif getSecondNum(arr[i - 1]) == getSecondNum(arr[i]):
if getFirstNum(arr[i - 1]) < getFirstNum(arr[i]):
swap(i - 1, i)
swapped = True
return arr
a = ['1_1.json.gz',
'1_22.json.gz',
'7_33.json.gz',
'1_4.json.gz',
'2_1.json.gz',
'2_22.json.gz',
'9_33.json.gz',
'2_4.json.gz']
print(custom_bubble_sort(a))