使用运行numb的子字符串文本

2024-05-14 08:01:07 发布

您现在位置:Python中文网/ 问答频道 /正文

这应该是非常简单和简短的,但我想不出一个好的和简短的方法来做到这一点:
我有一个字符串,例如:

'How many roads must a man walk down Before you call him a man? How many seas must a white dove sail Before she sleeps in the sand? Yes, and how many times must the cannon balls fly Before they're forever banned?'

我想把一个单词“how”加上一个连续的数字,这样我就得到:

'[1] many roads must a man walk down Before you call him a man? [2] many seas must a white dove sail Before she sleeps in the sand? Yes, and [3] many times must the cannon balls fly Before they're forever banned?'


Tags: theyoucalldovemanyhowwalkdown
3条回答

可以将^{}与替换函数一起使用。函数将查找该单词在字典中出现的频率,并返回相应的数字。你知道吗

counts = collections.defaultdict(int)
def subst_count(match):
    word = match.group().lower()
    counts[word] += 1
    return "[%d]" % counts[word]

示例:

>>> text = "How many ...? How many ...? Yes, and how many ...?"
>>> re.sub(r"\bhow\b", subst_count, text, flags=re.I)
'[1] many ...? [2] many ...? Yes, and [3] many ...?'

注意:这对每个要替换的单词使用不同的计数(如果您使用的正则表达式匹配了多个单词),但不会重置对re.sub的调用之间的计数。你知道吗

可以使用itertools.count和函数作为替换参数,例如:

import re
from itertools import count

text = '''How many roads must a man walk down Before you call him a man? How many seas must a white dove sail Before she sleeps in the sand? Yes, and how many times must the cannon balls fly Before they're forever banned?'''
result = re.sub(r'(?i)\bhow\b', lambda m, c=count(1): '[{}]'.format(next(c)), text)
# [1] many roads must a man walk down Before you call him a man? [2] many seas must a white dove sail Before she sleeps in the sand? Yes, and [3] many times must the cannon balls fly Before they're forever banned?

下面是另一种使用re.sub替换函数的方法。但与使用全局对象来跟踪计数不同,这段代码使用的是函数属性。你知道吗

import re

def count_replace():
    def replace(m):
        replace.count += 1
        return '[%d]' % replace.count
    replace.count = 0
    return replace

src = '''How many roads must a man walk down Before you call him a man? How many seas must a white dove sail Before she sleeps in the sand? Yes, and how many times must the cannon balls fly Before they're forever banned?'''

pat = re.compile('how', re.I)

print(pat.sub(count_replace(), src))

输出

[1] many roads must a man walk down Before you call him a man? [2] many seas must a white dove sail Before she sleeps in the sand? Yes, and [3] many times must the cannon balls fly Before they're forever banned?

如果您只需要替换完整单词而不是部分单词,那么您需要一个更智能的regex,例如r"\bhow\b"。你知道吗

相关问题 更多 >

    热门问题