Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数调用将列表中的字母字符转换为数字_Python_Regex_Iterable - Fatal编程技术网

Python 函数调用将列表中的字母字符转换为数字

Python 函数调用将列表中的字母字符转换为数字,python,regex,iterable,Python,Regex,Iterable,我正在尝试Soundex算法的手动实现,这需要将字母文本字符转换为数字文本字符。我定义了以下函数: import re def sub_pattern(text): sub = [str(i) for i in range(1,4)] string = text abc = re.compile('[abc]') xyz = re.compile('[xyz]') encode = [abc, xyz] encode_iter = iter(

我正在尝试Soundex算法的手动实现,这需要将字母文本字符转换为数字文本字符。我定义了以下函数:

import re

def sub_pattern(text):
    sub = [str(i) for i in range(1,4)]
    string = text

    abc = re.compile('[abc]')
    xyz = re.compile('[xyz]')

    encode = [abc, xyz]
    encode_iter = iter(encode)

    alpha_search = re.compile('[a-zA-Z]')

    for i in sub:
        if alpha_search.search(string):
            pattern = next(encode_iter)
            string = pattern.sub(i, string)
        else:
            return(string)
此功能将
abc
字符编码为
1
,将
xyz
字符编码为
2
。但是,它只适用于单个字符串,我需要将字符串列表传递给函数。我已经通过以下方式获得了想要的结果:

list(map(sub_pattern, ['aab', 'axy', 'bzz']
但是我希望能够将列表直接传递给函数。我尝试过这个方法,但没有成功,因为它只返回列表中的第一个字符串

def sub_pattern(text_list):
    all_encoded = []
    sub = [str(i) for i in range(1,4)]

    abc = re.compile('[abc]')
    xyz = re.compile('[xyz]')

    encode = [abc, xyz]
    encode_iter = iter(encode)

    alpha_search = re.compile('[a-zA-Z]')

    for string in text_list:
        for i in sub:
            if alpha_search.search(string):
                pattern = next(encode_iter)
                string = pattern.sub(i, string)
            else:
                all_encoded.append(string)
有几件事需要注意:

  • 因为我正在实现Soundex算法,所以编码时文本的顺序很重要。我更愿意在原始索引处更新字符串,以避免以后重新组织它。换句话说,您不能对字符串进行任何排序……我已经创建了迭代器来递增地更新字符串,它只在所有字符尚未转换的情况下获取下一个
    regex
    模式
  • 这个函数将是我正在创建的两个自定义类的一部分。两者都将调用
    \uuuu iter\uuuu
    方法,以便我可以创建iterable。这就是为什么我使用
    iter()
    函数来创建iterable,因为如果迭代器自动运行,它将创建一个新实例 我知道相对于我所做的事情来说,这似乎是一个微不足道的问题,但我被卡住了


    先谢谢你

    我假设您的示例的问题是,一旦遍历了迭代器,下一个字符串就会遇到StopIteration

    我不确定这是否是您想要的,但我会为每个字符串创建一个新的迭代器,因为您必须能够遍历每个新项的所有迭代器。我调整了一些可能导致混淆的变量名(string和sub)。有关更改,请参见注释:

    def子模式(文本列表):
    所有_编码=[]
    数字=[str(i)表示范围(1,4)内的i]
    abc=重新编译(“[abc]”)
    xyz=re.compile(“[xyz]”)
    编码=[abc,xyz]
    alpha_search=re.compile(“[a-zA-Z]”)
    对于文本列表中的项目:
    #为每个字符串创建新的迭代器。
    encode_iter=iter(encode)
    对于以数字表示的i:
    如果alpha_搜索。搜索(项目):
    模式=下一个(编码)
    项目=模式子(i,项目)
    其他:
    所有_编码。追加(项)
    #一旦找不到更多的字母,您可能希望将其追加到结尾。
    打破
    #返回编码文本。
    返回所有已编码的
    
    测试:

    打印(子模式(['aab',axy',bzz'])#输出:['111',122',122']
    
    递归使用自己的函数怎么样?您可以将原件保持原样,以防需要:

    import re
    
    def sub_pattern(text):
        if isinstance(text, str):
            sub = [str(i) for i in range(1,4)]
            string = text
    
            abc = re.compile('[abc]')
            xyz = re.compile('[xyz]')
    
            encode = [abc, xyz]
            encode_iter = iter(encode)
    
            alpha_search = re.compile('[a-zA-Z]')
    
            for i in sub:
                if alpha_search.search(string):
                    pattern = next(encode_iter)
                    string = pattern.sub(i, string)
                else:
                    return(string)
        else:
            return([sub_pattern(t) for t in text])
    
    
    print(list(map(sub_pattern, ['aab', 'axy', 'bzz']))) # old version still works
    print(sub_pattern(['aab', 'axy', 'bzz'])) # new version yields the same result
    

    如果读者不知道这意味着什么:从函数本身调用函数

    • 这是允许的,因为每个函数调用都创建自己的 范围,
    • 当您可以通过多次执行简单操作来解决问题,或者无法提前预测需要执行多少次才能获得解决方案时,例如,当您需要解包嵌套结构时,此功能非常有用
    • 它是通过选择基本情况(解决方案)来定义的,并在所有其他情况下调用该函数,直到达到基本情况为止