I have a list of string.
A = [
'kite1.json',
'kite1.mapping.json',
'kite1.analyzer.json',
'kite2.json',
'kite3.mapping.json',
'kite3.mapping.mapping.json',
'kite3.mapping.analyzer.json',
]
I need to find common prefix which ends with all of .json, .mapping.json, .analyzer.json.
Here, kite1 & kite3.mapping are satisfied. But kite2 isn't, because it only ends with .json.
How can I find those prefix which ends with all of .json, .mapping.json, .analyzer.json.
If this were code-golf, I might win:
def ew(sx):
return set([s[:-len(sx)] for s in A if s.endswith(sx)])
ew('.analyzer.json') & ew('.mapping.json') & ew('.json')
The ew() function loops through A, finding all elements that end with the given suffix and stripping the suffix off, returning the results at a set.
Using it, I just calculate the intersection of the sets produced from each of the three suffixes. (& is the operator for intersection.)
For brevity's sake, I abbreviated "ends with" to ew and "suffix" to sx.
The expression s[:-len(sx)] means "the substring of s starting at 0 and going to len(sx) characters from the end", which has the effect of the snipping suffix off the end.
Well, all you need is to collect a set of prefixes for each suffix in ['.json', '.mapping.json', '.analyzer.json'] and then just take an intersection of these sets:
In [1]: A = [
...: 'kite1.json',
...: 'kite1.mapping.json',
...: 'kite1.analyzer.json',
...: 'kite2.json',
...: 'kite3.mapping.json',
...: 'kite3.mapping.mapping.json',
...: 'kite3.mapping.analyzer.json',
...: ]
In [2]: suffixes = ['.json', '.mapping.json', '.analyzer.json']
In [3]: prefixes = {s: set() for s in suffixes}
In [4]: for word in A:
....: for suffix in suffixes:
....: if word.endswith(suffix):
....: prefixes[suffix].add(word[:-len(suffix)])
....:
In [5]: prefixes
Out[5]:
{'.analyzer.json': {'kite1', 'kite3.mapping'},
'.json': {'kite1',
'kite1.analyzer',
'kite1.mapping',
'kite2',
'kite3.mapping',
'kite3.mapping.analyzer',
'kite3.mapping.mapping'},
'.mapping.json': {'kite1', 'kite3', 'kite3.mapping'}}
In [6]: prefixes['.json'] & prefixes['.mapping.json'] & prefixes['.analyzer.json']
Out[6]: {'kite1', 'kite3.mapping'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With