简体   繁体   中英

How to group files based on similarly positioned characters/pattern in Python?

I have a set of file names in a list in different folders as shown below:

Input files

['ABC.dat',
'ABC10.dat',
'ABC956.dat',
'ABC_DEF_1.dat',
'ABC_DEF_2.dat',
'ABC_DEF_3.dat',
'ABC10_DEF_1.dat',
'ABC10_DEF_2.dat',
'ABC10_DEF_3.dat',
'ABC956_DEF_1.dat',
'ABC956_DEF_2.dat',
'ABC956_DEF_3.dat',
'XYZ_ABC_1.dat',
'XYZ_ABC_2.dat',
'XYZ10_ABC_1.dat',
'XYZ10_ABC_2.dat',
'XYZ956_ABC_1.dat',
'XYZ956_ABC_2.dat',
'XYZ_PQR_JKL.dat',
'XYZ_PQR_JKL_1.dat',
'XYZ_PQR10_JKL.dat',
'XYZ_PQR10_JKL_1.dat',
'XYZ_PQR956_JKL.dat',
'XYZ_PQR956_JKL_1.dat'] 

I would like to group the files as follows:

Output list

[['ABC.dat', 'ABC10.dat', 'ABC956.dat'],
['ABC_DEF_1.dat', 'ABC10_DEF_1.dat.dat', 'ABC956_DEF_1.dat'],
['ABC_DEF_2.dat', 'ABC10_DEF_2.dat.dat', 'ABC956_DEF_2.dat'],
['ABC_DEF_3.dat', 'ABC10_DEF_3.dat.dat', 'ABC956_DEF_3.dat'],
['XYZ_ABC_1.dat', 'XYZ10_ABC_1.dat', 'XYZ956_ABC_1.dat'],
['XYZ_ABC_2.dat', 'XYZ10_ABC_2.dat', 'XYZ956_ABC_2.dat'],
['XYZ_PQR_JKL.dat', 'XYZ_PQR10_JKL.dat', 'XYZ_PQR956_JKL.dat'],
['XYZ_PQR_JKL_1.dat', 'XYZ_PQR10_JKL_1.dat', 'XYZ_PQR956_JKL_1.dat']]

That is to say the files should be grouped based on the pattern of files. Note DEF_1 and DEF_2 have to be grouped separately. The numbers 10, 956 are random, ie, they are not known before hand. A MWE is given below, which groups based on first few letters as obtained from OP , how can I extend it to the other letters that is DEF.

MWE

import os
import random
import errno
import itertools
from itertools import repeat

#--------------------------------------
# Main rename code
for root, dirs, files in os.walk('./input_folder'):
    for dir in dirs: 
        print (dir)
        output_files = [s for s in os.listdir(os.path.join(root,dir)) if s.endswith('.dat')]
        groups =  [list(g) for _, g in itertools.groupby(sorted(output_files), lambda x: x[0:2])]    # obtained from Aaron's answer https://gis.stackexchange.com/a/206053
        print (groups)

You can use recursion:

import re
def is_match(a, b):
  a, b = re.sub('\s\w+\.dat$', '', a).split(), re.sub('\s\w+\.dat$', '', b).split()
  if len(a) != len(b):
     return False
  return all(c == d if not c.isdigit() and not d.isdigit() else c.isdigit() and d.isdigit() for c, d in zip(a, b))

def group_vals(d, _current = []):
  if _current:
    yield _current
  if d:
    _start, *_d = d
    yield from group_vals([i for i in _d if not is_match(_start, i)], [_start, *[i for i in _d if is_match(_start, i)]])

files = list(filter(None, _input.split('\n')))
print(list(group_vals(files)))

Output:

[['ABC 956.dat', 'ABC 114.dat', 'ABC 577.dat', 'ABC 782.dat'], 
 ['ABC DEF 10.dat', 'ABC DEF 23.dat', 'ABC DEF 27.dat', 'ABC DEF 54.dat'], 
  ['XYZ-ABC 158.dat', 'XYZ-ABC 221.dat', 'XYZ-ABC 668.dat', 'XYZ-ABC 919.dat'], 
  ['ABC 127 JKL.dat', 'ABC 272 JKL.dat', 'ABC 462 JKL.dat', 'ABC 707 JKL.dat'], 
  ['ABC 137 XYZ 97.dat', 'ABC 164 XYZ 25.dat', 'ABC 418 XYZ 13.dat', 'ABC 913 XYZ 11.dat'], 
  ['ABC 258 PQR0 0.dat', 'ABC 551 PQR0 3.dat', 'ABC 606 PQR0 5.dat', 'ABC 654 PQR0 2.dat'], 
  ['ABC 542 PQR1 4.dat', 'ABC 234 PQR1 2.dat', 'ABC 432 PQR1 7.dat', 'ABC 766 PQR1 5.dat']]

You probably should try Regular Expression in Python (re library).

re.findall (pattern, string, flags=0)
Return all non-overlapping matches of pattern in string, as a list of strings.

# suppose files is a string holds all your file names (you could join your file names together)
files = """ABC 956.dat
ABC DEF 10.dat
ABC DEF 23.dat
ABC DEF 27.dat
ABC DEF 54.dat
XYZ-ABC 158.dat
XYZ-ABC 221.dat
XYZ-ABC 668.dat
XYZ-ABC 919.dat"""

# use re to find the names with certain pattern.
import re
g1 = re.findall('ABC \d{3}.dat', files)
# ['ABC 956.dat', 'ABC 158.dat', 'ABC 221.dat', 'ABC 668.dat', 'ABC 919.dat']
g2 = re.findall('ABC DEF \d{2}.dat', files)
# ['ABC DEF 10.dat', 'ABC DEF 23.dat', 'ABC DEF 27.dat', 'ABC DEF 54.dat']

# more groups to go with similar settings

In the code example, \\d represents one digit and {n} represents the number of occurrence of the previous pattern. Thus \\d{3} means 3 digits.

You could get more information about regular expression here .

This is based on Ajax1234's answer. It avoids that answer's redundant computation. Rather than doing a recursive partition by an equivalence relation. This does discrimination. This reduces the cost from N**2/2 calls to is_match to only N calls to key . key uses None as a wildcard for the parts of the filename that are digits.

import re
from collections import defaultdict

def key(v):
    return tuple(None if p.isdigit() else p for p in re.sub('.dat$', '', v).split())

def partition(l, key=None):
    d = defaultdict(list)
    for e in l:
        k = key(e) if key is not None else e
        d[k].append(e)
    return [d[k] for k in sorted(d)]

partition(filter(None, _input.split('\n')), key=key)

This results in:

[['ABC 956.dat', 'ABC 114.dat', 'ABC 577.dat', 'ABC 782.dat'],
 ['ABC 127 JKL.dat', 'ABC 272 JKL.dat', 'ABC 462 JKL.dat', 'ABC 707 JKL.dat'],
 ['ABC 258 PQR0 0.dat', 'ABC 551 PQR0 3.dat', 'ABC 606 PQR0 5.dat', 'ABC 654 PQR0 2.dat'],
 ['ABC 542 PQR1 4.dat', 'ABC 234 PQR1 2.dat', 'ABC 432 PQR1 7.dat', 'ABC 766 PQR1 5.dat'],
 ['ABC 137 XYZ 97.dat', 'ABC 164 XYZ 25.dat', 'ABC 418 XYZ 13.dat', 'ABC 913 XYZ 11.dat'],
 ['ABC DEF 10.dat', 'ABC DEF 23.dat', 'ABC DEF 27.dat', 'ABC DEF 54.dat'],
 ['XYZ-ABC 158.dat', 'XYZ-ABC 221.dat', 'XYZ-ABC 668.dat', 'XYZ-ABC 919.dat']]

It seems I wasn't clear enough on where to make the changes:

def key(v):
    return tuple(None if p.isdigit() else p for p in re.sub('.dat$', '', v).split('_'))

partition(filter(None, input_list), key=key)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM