Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce list in python according to common items

Tags:

python

I want to make such transformation:

['test.smth.test', 'test.smth'] -> ['test.smth']
['test.smth.test', 'test.smth.another'] -> ['test.smth.test', 'test.smth.another']
['test.one', 'test.smth'] -> ['test.one', 'test.smth']
['test.one', 'test', 'test.smth.name'] -> ['test']
['test_another.one.name', 'test', 'test.smth.name'] -> ['test', 'test_another.one.name']

I ended up with this code:

def format_fields(fields):
    fields_data = defaultdict(list)
    for field in fields:
        split = field.split('.')
        base = split[0]
        already = False
        for i in reversed(range(len(split))):
            if split[:i] in fields_data[base]:
                already = True
                break
        if already:
            continue
        current = [i for i in fields_data[base] if len(i) < len(split)
                   or i[len(split) - 1] != split[-1]]
        fields_data[base] = current + [split]
    return ['.'.join(value) for group in fields_data.values() for value in group]

It seems to work, but is there a more readable/clever solution, or a third-party library that can do this?

like image 986
Vova Avatar asked Jan 23 '26 00:01

Vova


1 Answers

This should work, basically you need to find every field that's not contained in any other field, adding a dot to the end of every field save you from substring mistake like 'test_another' and 'test':

cases = [
  ['test.smth.test', 'test.smth'],
  ['test.smth.test', 'test.smth.another'],
  ['test.one', 'test.smth'],
  ['test.one', 'test', 'test.smth.name'],
  ['test_another.one.name', 'test', 'test.smth.name']
]

def filterFields(fields):
  cFields = [field + '.' for field in fields]
  return [field[:-1] for index, field in enumerate(cFields) if all(field.find(f) != 0 for f in cFields[:index] + cFields[index+1:])]

for case in cases:
  print(case, '->', filterFields(case))

WORKING CODE

like image 90
Vanojx1 Avatar answered Jan 27 '26 02:01

Vanojx1