代码之家  ›  专栏  ›  技术社区  ›  Ha Bom

用字符替换单词中的数字

  •  3
  • Ha Bom  · 技术社区  · 5 年前

    s ="Question1: a12 is the number of a, 1b is the number of b"
    

    使用 x = re.compile('\w+').findall(s) 我可以得到

    ['Question1', 'a12', 'is', 'the', 'number', 'of', 'a', '1b', 'is', 'the', 'number', 'of', 'b']
    

    现在我想用一个词来代替这个数字,例如,

    • Question1 -> Question$
    • a12 1b -> a$ , $b

    我试过了 y = [re.sub(r'\w*\d\w*', '$', x) for w in x]

    $ :

    ['$', '$', 'is', 'the', 'number','of', 'a', '$', 'is', 'the', 'number', 'of', 'b']
    

    4 回复  |  直到 5 年前
        1
  •  2
  •   Allan    5 年前

    您可以调整以下示例以满足您的要求:

    如果要替换的数字仅位于单词末尾:

    import re
    
    s = "Question1: a12 is the number of a, 1b is the number of b, 123"
    x = re.compile('\w+').findall(s)
    y = [re.sub(r'(?<=[a-zA-Z])\d+$', '$', w) for w in x]
    print(y)
    

    输出:

    ['Question$', 'a$', 'is', 'the', 'number', 'of', 'a', '1b', 'is', 'the', 'number', 'of', 'b', '123']
    

    (结果为字符串形式):

    import re
    s ="Question1: a12 is the number of a, 1b is the number of b, abc1uvf"
    pat = re.compile(r'(?<=[a-zA-Z])\d+(?=\W)')
    print(re.sub(pat, "$", s))
    

    输出:

    Question$: a$ is the number of a, 1b is the number of b, abc1uvf
    

    如果数字可以位于单词中的任何位置,请使用:

    import re
    
    s = "Question1: a12 is the number of a, 1b is the number of b, 123"
    x = re.compile('\w+').findall(s)
    y = [re.sub(r'\d+', '$', w) for w in x]
    print(y)
    

    输出:

    ['Question$', 'a$', 'is', 'the', 'number', 'of', 'a', '$b', 'is', 'the', 'number', 'of', 'b', '$']
    

    123 被替换为 $ ,如果这不是您想要的用途:

    import re
    
    s = "Question1: a12 is the number of a, 1b is the number of b, 123"
    x = re.compile('\w+').findall(s)
    y = [re.sub(r'(?<=[a-zA-Z])\d+|\d+(?=[a-zA-Z])', '$', w) for w in x]
    print(y)
    

    ['Question$', 'a$', 'is', 'the', 'number', 'of', 'a', '$b', 'is', 'the', 'number', 'of', 'b', '123']
    

    一步到位:

    import re
    
    s = "Question1: a12 is the number of a, 1b is the number of b, 123"
    y = re.sub(r'(?<=[a-zA-Z])\d+|\d+(?=[a-zA-Z])', '$', s)
    print(y)
    
        2
  •  1
  •   Xosrov    5 年前

    试试这个:

    import re
    s ="Question1: a12 is the number of a, 1b is the number of b"
    pat = re.compile("[0-9]+")
    print(re.sub(pat, "$", s))
    
        3
  •  1
  •   Ora Aff    5 年前

    试试这个:

    import re
    x = ['Question1', 'a12', 'is', 'the', 'number', 'of', 'a', '1b', 'is', 'the', 'number', 'of', 'b']
    y = [re.sub(r'\d+', '$', w) for w in x]
    print(y)
    

    ['Question$', 'a$', 'is', 'the', 'number', 'of', 'a', '$b', 'is', 'the', 'number', 'of', 'b']
    
        4
  •  1
  •   shaik moeed    5 年前

    说明:

    • re.sub 是要替换的数字。

      \d+ 找到数字, + 代表一个或多个事件

    • 第二个参数采用什么来替换模式。在这种情况下 它的 '$'

    • 第三个参数接受输入字符串。

    这是你想要的:

    import re
    s ="Question1: a12 is the number of a, 1b is the number of b"
    print(re.sub('\d+', '$', s))
    

    输出:

    Question$: a$ is the number of a, $b is the number of b