Python将多个字符串同时添加到具有索引的一个字符串中

问题描述 投票:0回答:2

我有一个长文本,还有一些dict对象的列表,这些对象具有此长文本的索引。我想向这些索引添加一些字符串。如果设置循环,索引会更改,并且必须再次计算索引。我认为这种方式非常令人困惑。有没有办法一次将不同的字符串添加到不同的索引?

我的样本数据:

main_str = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'

我的indexes列表:

indexes_list = [
    {
      "type": "first_type",
      "endOffset": 5,
      "startOffset": 0,
    },
    {
      "type": "second_type",
      "endOffset": 22,
      "startOffset": 16,
    }
]

我的主要目的:我想为基于类型的某些颜色样式的给定索引添加<span>属性。之后,我直接在模板上渲染它。还有其他建议吗?

例如,我想根据上述变量main_strindexes_list创建此数据(请忽略样式的color部分。我根据type中的indexes_list的值动态提供它):

new_str = '<span style="color:#FFFFFF">Lorem</span> Ipsum is <span style="color:#FFFFFF">simply</span> dummy text of the printing and typesetting industry.'
python django python-3.x string string-parsing
2个回答
1
投票

创建一个新的str以避免更改main_str:

main_str = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'
indexes_list = [
    {
      "type": "first_type",
      "startOffset": 0,
      "endOffset": 5,
    },
    {
      "type": "second_type",
      "startOffset": 16,
      "endOffset": 22,
    }
]

new_str = ""
index = 0
for i in indexes_list:
    start = i["startOffset"]
    end = i["endOffset"]
    new_str += main_str[index: start] + "<span>" + main_str[start:end] + "</span>"
    index = end
new_str += main_str[index:]
print(new_str)

0
投票
# Get all the indices and label them as starts or ends.
starts = [(o['startOffset'], True) for o in indexes_list]
ends = [(o['endOffset'], False) for o in indexes_list]

# Sort everything...
all_indices = sorted(starts + ends)

# ...so it is possible zip together adjacent pairs and extract substrings.
pieces = [
    (s[1], main_str[s[0]:e[0]])
    for s, e in zip(all_indices, all_indices[1:])
]

# And then join all the pieces together with a bit of conditional formatting.
formatted = ''.join([
    f"<span>{part}</span>" if is_start else part
    for is_start, part in pieces
])

formatted
# '<span>Lorem</span> Ipsum is s<span>imply </span>dummy text of the printing and typesetting industry.'
© www.soinside.com 2019 - 2024. All rights reserved.