我有一个这样的文本:
text = "All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood."
我如何编写一个功能对冲(文本),它处理我的文本并产生一个新版本,在文本的每三个单词中插入单词"like"?
结果应该是这样的:
text2 = "All human beings like are born free like and equal in like..."
谢谢!
而不是像
solution=' like '.join(map(' '.join, zip(*[iter(text.split())]*3)))
我发布了一个关于如何处理这个问题的一般性建议。"算法"不是特别"python",但希望易于理解:
words = split text into words
number of words processed = 0
for each word in words
output word
number of words processed += 1
if number of words processed is divisible by 3 then
output like
你可以这样写:
' '.join([n + ' like' if i % 3 == 2 else n for i, n in enumerate(text.split())])