在tex中插入单词的函数

2024-09-27 00:21:20 发布

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

我有一段文字是这样写的:

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."

如何编写一个函数对冲(text)来处理我的文本并生成一个新版本,在文本的每三个单词中插入单词“like”?在

结果应该是这样的:

^{pr2}$

谢谢你!在


Tags: andtextin文本freeequalall单词
2条回答

你可以这样做:

' '.join([n + ' like' if i % 3 == 2 else n for i, n in enumerate(text.split())])

而不是给你

  solution=' like '.join(map(' '.join, zip(*[iter(text.split())]*3)))

我在贴一个关于如何解决这个问题的一般建议。“算法”并不是特别的“Python”,但希望很容易理解:

^{pr2}$

如果您有任何问题,请告诉我们。在

相关问题 更多 >

    热门问题