Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active August 16, 2018 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/611322f255afd7e07c81c5619d98d95d to your computer and use it in GitHub Desktop.
Save CMCDragonkai/611322f255afd7e07c81c5619d98d95d to your computer and use it in GitHub Desktop.
Common String Components #python
from itertools import zip_longest
def common_string_components(strings, separator=None):
def split_string(string):
if type(string) == str:
# if separator is '', split by character
if separator == '':
string = list(string)
else:
string = string.split(separator)
return string
strings = map(split_string, strings)
strings = zip_longest(*strings)
strings = map(lambda c: list(set(c)), strings)
return list(strings)
# it is possible to later use this for producing canonical names
# from a set of possible names, you just have to replace `None` with `'None'`
# each component can be separated by spaces
# so "A B C" and "A B D" can turn into "A B (C or D)"
def canonical_name(names, separator=None):
name_components = common_string_components(names, separator)
name_components_rendered = []
for component in name_components:
component = ['None' if c is None else c for c in component]
if len(component) > 1:
component = '(' + ' or '.join(component) + ')'
else:
component = ''.join(component)
name_components_rendered.append(component)
name_components_rendered = ' '.join(name_components_rendered)
return name_components_rendered
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment