使用位置在 python 字符串中插入标签

发布于 2025-01-14 05:19:21 字数 518 浏览 4 评论 0原文

我有一个 python 字符串和一个元组/元组列表,表示要标记的子序列的位置和标记的名称:

text = "This is an example"
label = (0, 4, "tag1")

# Expected result
> "<tag1>This</tag1> is an example"

另一个示例:

text = "This is a more complicated example"
labels = [(0, 7, "tag1"),
          (5, 7, "tag2"),
          (10, 14, "tags3")]

# Expected result
> "<tag1>This <tag2>is</tag2></tag1> a <tag3>more</tag3> complicated example"

如何在指定位置插入标记?

I have a python string and a tuple/list of tuples representing the position of the subsequence to tag and the name of the tag:

text = "This is an example"
label = (0, 4, "tag1")

# Expected result
> "<tag1>This</tag1> is an example"

Another example:

text = "This is a more complicated example"
labels = [(0, 7, "tag1"),
          (5, 7, "tag2"),
          (10, 14, "tags3")]

# Expected result
> "<tag1>This <tag2>is</tag2></tag1> a <tag3>more</tag3> complicated example"

How to insert a tag at the specified positions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文