you can use os.path.commonprefixto calculate the common prefix. It is used to calculate shared directories in a list of file paths, but it can be used in a general context.
, , ( https://gist.github.com/willwest/ca5d050fdf15232a9e67)
dataset = """id.4030.paid
id.1280.paid
id.88.paid""".splitlines()
import os
def longest_common_suffix(list_of_strings):
reversed_strings = [s[::-1] for s in list_of_strings]
return os.path.commonprefix(reversed_strings)[::-1]
common_prefix = os.path.commonprefix(dataset)
common_suffix = longest_common_suffix(dataset)
print("{}*{}".format(common_prefix,common_suffix))
:
id.*.paid
EDIT: wim:
- , ,
prefix*suffix: , - / , :
, , , ( / )
def compute_generic_string(dataset):
if len(set(dataset))==1:
return dataset[0]
commonprefix = os.path.commonprefix(dataset)
return "{}*{}".format(commonprefix,os.path.commonprefix([s[len(commonprefix):][::-1] for s in dataset])[::-1])
:
for dataset in [['id.4030.paid','id.1280.paid','id.88.paid'],['aBBc', 'aBc'],[]]:
print(compute_generic_string(dataset))
:
id.*.paid
aB*c
*
( , *, , )