Python 识别以文字形式书写的数字序列

Python 识别以文字形式书写的数字序列,python,python-3.x,list,Python,Python 3.x,List,我有python中的单词列表。在列表元素中,我将数字写成文字。例如: list = ['man', 'ball', 'apple', 'thirty-one', 'five', 'seven', 'twelve', 'queen'] n_dict = {'zero':0, 'one':1, 'two':2, ...., 'hundred':100} list2 = ['bike', 'earth', 't-shirt', 'twenty-five', 'zero', 'seven', 'ho

我有python中的单词列表。在列表元素中,我将数字写成文字。例如:

list = ['man', 'ball', 'apple', 'thirty-one', 'five', 'seven', 'twelve', 'queen']
n_dict = {'zero':0, 'one':1, 'two':2, ...., 'hundred':100}
list2 = ['bike', 'earth', 't-shirt', 'twenty-five', 'zero', 'seven', 'home', 'bottle']
list3 = ['stairs', 'tree', 'street', 'forty-two', 'nine', 'submarine', 'two', 'eighty-five']
我还有一本字典,每一个数字都以单词作为键,对应的数字作为值。例如:

list = ['man', 'ball', 'apple', 'thirty-one', 'five', 'seven', 'twelve', 'queen']
n_dict = {'zero':0, 'one':1, 'two':2, ...., 'hundred':100}
list2 = ['bike', 'earth', 't-shirt', 'twenty-five', 'zero', 'seven', 'home', 'bottle']
list3 = ['stairs', 'tree', 'street', 'forty-two', 'nine', 'submarine', 'two', 'eighty-five']
我需要做的是识别4个或更多(大于4个)数字,这些数字在列表中连续写为单词,并根据字典将它们转换为数字。例如,列表应如下所示:

list = ['man', 'ball', 'apple', '31', '5', '7', '12', 'queen']
但是,如果连续元素少于规定的数量(在我们的案例4中),则列表应相同。例如:

list = ['man', 'ball', 'apple', 'thirty-one', 'five', 'seven', 'twelve', 'queen']
n_dict = {'zero':0, 'one':1, 'two':2, ...., 'hundred':100}
list2 = ['bike', 'earth', 't-shirt', 'twenty-five', 'zero', 'seven', 'home', 'bottle']
list3 = ['stairs', 'tree', 'street', 'forty-two', 'nine', 'submarine', 'two', 'eighty-five']
清单2应保持原样

此外,如果有多个数字以文字形式写入的序列,但它们未达到所需的最小连续字数,则文字不应变为数字。例如:

list = ['man', 'ball', 'apple', 'thirty-one', 'five', 'seven', 'twelve', 'queen']
n_dict = {'zero':0, 'one':1, 'two':2, ...., 'hundred':100}
list2 = ['bike', 'earth', 't-shirt', 'twenty-five', 'zero', 'seven', 'home', 'bottle']
list3 = ['stairs', 'tree', 'street', 'forty-two', 'nine', 'submarine', 'two', 'eighty-five']
清单3应保持原样

以文字形式书写的数字序列可以在列表中的任何位置。在开始,在最后,在某个中间。

到目前为止,我所尝试的:

def checkConsecutive(l): 
    return sorted(l) == list(range(min(l), max(l)+1))

def replace_numbers(word_list, num_dict):

    flag = False

    intersect = list(set(word_list) & set(n_dict.keys()))

    intersect_index = [word_list.index(elem) for elem in intersect]

    flag = check_if_consecutive(intersect_index)

    if (len(intersect_index) > 4) & flag:
    
       flag = True
       for index in intersect_index:
        
         word_list[index] = n_dict[word_list[index]]

return word_list, flag
我还需要返回标志以跟踪哪些列表发生了更改

上面的代码工作得很好,但我认为它没有那么有效。我的问题是能否以更好的方式实施。例如,使用或以类似方式使用某物。

表示数字

来自itertools导入过滤器False
列表\u字符串\u是\u整数=[*filterfalse(lambda x:isinstance(x,bool),(n\u dict.get(i,False)表示列表\u字符串中的i))]
对于连续性,以下内容适用于任何索引候选项

def连续(候选,差速器=1):
返回全部(e==候选[i-1]+枚举中i,e的差分(候选[1:]))

您尝试过什么?还有,你的问题是什么?堆栈溢出不仅仅是为了提供代码;你需要问一个特定的问题,关于一个特定的问题。对不起@M-Chen-3。我没有发表过很多文章,所以我已经认为我的代码太难看了,无法发表。我编辑了这篇文章。很抱歉@Aven Desta。这不是一个家庭作业,这是我试图解决的问题的一部分,我把它转换成了这种列表处理操作。如上所述,我认为我的代码太难看了,无法发布。谢谢!这就是我所寻找的答案的精神所在。但是,这是作业的一部分,因为它返回数字,但不检查列表中的数字是否连续,即不检查字符串列表中的索引是否连续。我编辑了文章,希望有帮助。