Identify a sequence of numbers written as words

I have lists of words in python. In the list elements I have numbers written as words. For example:

list = ['man', 'ball', 'apple', 'thirty-one', 'five', 'seven', 'twelve', 'queen']

I have also the dictionary with every number written as word as the key and the corresponding digit as value. For example:

n_dict = {'zero':0, 'one':1, 'two':2, ...., 'hundred':100}

What I need to do is to identify let's say 4 or more (greater than 4) numbers written as words consecutively in the list and convert them to digits based on the dictionary. For example list should be like:

list = ['man', 'ball', 'apple', '31', '5', '7', '12', 'queen']

However, if there are less consecutive elements than the number specified (in our case 4) the list shall be the same. For example:

list2 = ['bike', 'earth', 't-shirt', 'twenty-five', 'zero', 'seven', 'home', 'bottle']

list2 Shall remain as it is.

In addition, if there are multiple sequences with numbers written as words but they are not reaching the minimum amount of consecutive words required the words should not change to digits. For example:

list3 = ['stairs', 'tree', 'street', 'forty-two', 'nine', 'submarine', 'two', 'eighty-five']

list3 Shall remain as it is.

The sequence of numbers written as words can be anywhere at the list. At the beginning, at the end, somewhere in the middle. Used language python 3



Read more here: https://stackoverflow.com/questions/66266445/identify-a-sequence-of-numbers-written-as-words

Content Attribution

This content was originally published by wannabedatasth at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: