如何按连续前缀将文件名分组到列表中?

How to group filenames into lists by the consecutive prefix?

我有多个具有相同后缀 (_01_020301.txt) 和不同数字前缀的文件。目标是通过连续的前缀将它们分组到列表中。

这是一个例子:

09456_01_020301.txt
09457_01_020301.txt
09458_01_020301.txt
09459_01_020301.txt
09460_01_020301.txt
09465_01_020301.txt
09466_01_020301.txt
09467_01_020301.txt
09468_01_020301.txt

因为09456~09460和09465~09468是连续的两组,所以结果应该是:

[['09456_01_020301.txt',
  '09457_01_020301.txt',
  '09458_01_020301.txt',
  '09459_01_020301.txt',
  '09460_01_020301.txt'],
 ['09465_01_020301.txt',
  '09466_01_020301.txt',
  '09467_01_020301.txt',
  '09468_01_020301.txt']]

这是使用简单循环的一种方法:

l = ['09456_01_020301.txt',
     '09457_01_020301.txt',
     '09458_01_020301.txt',
     '09459_01_020301.txt',
     '09460_01_020301.txt',
     '09465_01_020301.txt',
     '09466_01_020301.txt',
     '09467_01_020301.txt',
     '09468_01_020301.txt']


out = []
prev = float('-inf')
for i in l:
    prefix = int(i.partition('_')[0])
    if prefix == prev+1:
        out[-1].append(i)
    else:
        out.append([i])
    prev = prefix

输出:

[['09456_01_020301.txt',
  '09457_01_020301.txt',
  '09458_01_020301.txt',
  '09459_01_020301.txt',
  '09460_01_020301.txt'],
 ['09465_01_020301.txt',
  '09466_01_020301.txt',
  '09467_01_020301.txt',
  '09468_01_020301.txt']]

假设输入是一个 str(例如,.csv 文件),我通过比较以前的后缀和当前后缀创建了以下代码。

input = """09456_01_020301.txt
09457_01_020301.txt
09458_01_020301.txt
09459_01_020301.txt
09460_01_020301.txt
09465_01_020301.txt
09466_01_020301.txt
09467_01_020301.txt
09468_01_020301.txt"""

result = []  # for the final result
tmp_list = []  # for a temporary sub_list for consecutie suffix files

lines = input.split('\n')
tmp_list.append(lines[0])
prev_suffix = int(lines[0][:5].lstrip('0'))  # lstrip('0') to remove leading zero (for example, 09456 to 9456)

for line in lines[1:]:
    current_suffix = int(line[:5].lstrip('0'))
    if current_suffix == (prev_suffix + 1):  # 9457 == 0946+1 ?
        tmp_list.append(line)
    else:
        result.append(tmp_list)
        tmp_list = [line]
    prev_suffix = current_suffix
result.append(tmp_list)

print(result)
# [['09456_01_020301.txt', '09457_01_020301.txt', '09458_01_020301.txt', '09459_01_020301.txt', '09460_01_020301.txt'], ['09465_01_020301.txt', '09466_01_020301.txt', '09467_01_020301.txt', '09468_01_020301.txt']]

另一个解决方案,使用itertools.groupby

from itertools import groupby

lst = [
    "09456_01_020301.txt",
    "09457_01_020301.txt",
    "09458_01_020301.txt",
    "09459_01_020301.txt",
    "09460_01_020301.txt",
    "09465_01_020301.txt",
    "09466_01_020301.txt",
    "09467_01_020301.txt",
    "09468_01_020301.txt",
]

out = []
for _, g in groupby(enumerate(lst), lambda k: int(k[1].split("_")[0]) - k[0]):
    out.append([v for _, v in g])

print(out)

打印:

[
    [
        "09456_01_020301.txt",
        "09457_01_020301.txt",
        "09458_01_020301.txt",
        "09459_01_020301.txt",
        "09460_01_020301.txt",
    ],
    [
        "09465_01_020301.txt",
        "09466_01_020301.txt",
        "09467_01_020301.txt",
        "09468_01_020301.txt",
    ],
]