带有运行编号

时间:2015-10-15 12:35:43

标签: python string python-2.7 substring

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

  

'一个男人走多少路必须先叫你一个男人?怎么样   在她睡在沙滩之前,许多海洋必须是白色的鸽子吗?是,   在他们永远存在之前,炮弹必须飞多少次   禁止呢?”

我希望用一个单词对一个单词说“怎么样”,然后我得到:

  

'[1]在你称他为男人之前,一个男人必须走很多路吗? [2]   在她睡在沙滩之前,许多海洋必须是白色的鸽子吗?是,   并且[3]炮弹必须多次飞行才能永远   禁止呢?”

5 个答案:

答案 0 :(得分:3)

您可以将re.sub与替换功能一起使用。该函数将查找该词在字典中的显示频率并返回相应的数字。

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

答案 1 :(得分:3)

您可以使用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?

答案 2 :(得分:2)

这是使用替换功能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]在你称他为男人之前,一个男人必须走很多路吗? [2]   在她睡在沙滩之前,许多海洋必须是白色的鸽子吗?是,   并且[3]炮弹必须多次飞行才能永远飞行   禁止?

如果您只需 替换完整的单词而非部分单词,那么您需要更智能的正则表达式,例如r"\bhow\b"

答案 3 :(得分:0)

Test = '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 theyre forever banned?'

i = 0

while("How" in Test):
    new = "["+str(i)+"]"
    Test = Test.replace("How",new,i)
    i=i+1


print Test

<强>输出

[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 how many times must the cannon balls fly Before theyre forever banned?

答案 4 :(得分:0)

为了好玩,我想知道我是否可以使用递归解决这个问题,这就是我得到的:

def count_replace(s, to_replace, leng=0, count=1, replaced=[]):
    if s.find(' ') == -1:
        replaced.append(s)
        return ' '.join(replaced)
    else:
        if s[0:s.find(' ')].lower() == to_replace.lower():
            replaced.append('[%d]' % count)
            count += 1
            leng = len(to_replace)
        else:
            replaced.append(s[0:s.find(' ')])
            leng = s.find(' ')
        return count_replace(s[leng + 1:], to_replace, leng, count, replaced)

毋庸置疑,我不会推荐这个,因为它过于复杂,而且我认为无论如何都要分享它,这是非常低效的。