Michael Elliott Michael Elliott - 25 days ago 7
Python Question

Python - Iterations of character replacements

I apologize if this has been answered previously - I wasn't entirely sure how to explain/search for this so I couldn't find anything existing.

I'm going through a large list of strings and trying to find matches in another data set. The input data set is space separated, and the existing data set uses an inconsistent combination of underscores and camel-case.

I'm looking for a clean way to iterate across all possibilities of these combinations.
The simplest case is this:

Input: "Variant Type"

Desired Output: "Variant_Type", "VariantType"

I had been accomplishing this 2-word case by searching twice:

x = input.replace(' ','_')
# Search
x = x.replace('_','')
# Search again


But now that I'm realizing there are a lot of longer strings like:

Input: "Timeline Integration Enabled"

Desired output:

"Timeline_Integration_Enabled", "TimelineIntegration_Enabled", "Timeline_IntegrationEnabled", "TimelineIntegrationEnabled"

Is there a clever, Pythonic way to accomplish this?

Note: I know I could use something like difflib.get_close_matches(), but I was hoping to do that as a last pass over the data, prompting the user to make decisions on any fields that weren't clear.

Thanks in advance and let me know if you need any more details.

Answer
def iterate_replacements(input_data):
    if " " in input_data:
        yield from iterate_replacements(input_data.replace(" ", "", 1))
        yield from iterate_replacements(input_data.replace(" ", "_", 1))
    else:
        yield input_data

for s in iterate_replacements("Timeline Integration Enabled"):
    print(s)

Or, for 2.7, which doesn't support yield from:

def iterate_replacements(input_data):
    if " " in input_data:
        for x in iterate_replacements(input_data.replace(" ", "", 1)): yield x
        for x in iterate_replacements(input_data.replace(" ", "_", 1)): yield x
    else:
        yield input_data

for s in iterate_replacements("Timeline Integration Enabled"):
    print(s)

Result:

TimelineIntegrationEnabled
TimelineIntegration_Enabled
Timeline_IntegrationEnabled
Timeline_Integration_Enabled