这应该是非常简单和简短的,但我想不出一个好的和简短的方法来做到这一点:
我有一个字符串,例如:
'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?'
可以将^{} 与替换函数一起使用。函数将查找该单词在字典中出现的频率,并返回相应的数字。你知道吗
示例:
注意:这对每个要替换的单词使用不同的计数(如果您使用的正则表达式匹配了多个单词),但不会重置对
re.sub
的调用之间的计数。你知道吗可以使用
itertools.count
和函数作为替换参数,例如:下面是另一种使用
re.sub
替换函数的方法。但与使用全局对象来跟踪计数不同,这段代码使用的是函数属性。你知道吗输出
如果您只需要替换完整单词而不是部分单词,那么您需要一个更智能的regex,例如
r"\bhow\b"
。你知道吗相关问题 更多 >
编程相关推荐