Home > Enterprise >  How to group filenames into lists by the consecutive prefix?
How to group filenames into lists by the consecutive prefix?

Time:04-28

I have multiple files which have the same suffix (_01_020301.txt) and different number prefixes. The goal is to group them into lists by the consecutive prefix.

Here's an example:

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

Because 09456~09460 and 09465~09468 are two consecutive groups, the result should be:

[['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']]

CodePudding user response:

Here is one way using a simple loop:

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

output:

[['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']]

CodePudding user response:

With assumtion of the input is a str (for example, .csv file), I created following code by comparing previous suffix and current suffix.

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']]

CodePudding user response:

Another solution, using 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)

Prints:

[
    [
        "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",
    ],
]
  • Related