-1

I have a list of alphanumeric filenames, as follows:

['build999.100', 'build999.0', 'buil998.100', 
 'build997.100', 'build996.100', 'build996.0']

I want to get a list removing the duplicates of the suffix post '.', i.e. I want my list to be

['build999.100', 'buil998.100', 'build997.100', 'build996.100']

The suffix after '.' does not matter, I just need to remove the duplicates on the basis of 999, 998 etc.

I am looking for implementations in either Python or Unix.

vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
user8950257
  • 51
  • 1
  • 6
  • Does it matter which minor number build you keep? In your example you keep build999.100 over build999.0, any particular reason for 100 over 0? Or does it not matter? – Jesse Sep 25 '18 at 00:26
  • It does not matter. I just need one instance of them. – user8950257 Sep 25 '18 at 00:28

2 Answers2

1

A simple & efficient way to do this is to use a dictionary, with the prefix as the key, since the keys of a dict are unique.

data = ['build999.100', 'build999.0', 'buil998.100', 'build997.100', 'build996.100', 'build996.0']
d = {s.split('.')[0]: s for s in data}
out = list(d.values())
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • WOW this using a dictionary here abusing the fact can only have on key this is wonderful, tagging you in a question I want you to solve – vash_the_stampede Sep 25 '18 at 00:44
  • https://stackoverflow.com/questions/52473178/python3-how-to-compare-data-of-two-dictionaries-which-is-nested-and-dynamic-in going to my friends you better have solved this before I return – vash_the_stampede Sep 25 '18 at 00:46
  • @vash_the_stampede Sorry, I'm on my phone. And I need to get some sleep soon. – PM 2Ring Sep 25 '18 at 01:00
0

Use regular expressions:

import re
a = ['build999.100', 'build999.0', 'buil998.100', 
    'build997.100', 'build996.100', 'build996.0']
list(set(map(lambda x: re.sub(r'\..*$', '.100', x), a)))

or

list(set([re.sub(r'\..*$', '.100', x) for x in a]))

Result is

>> ['build996.100', 'build999.100', 'buil998.100', 'build997.100']
Michael
  • 5,095
  • 2
  • 13
  • 35