gpt4 book ai didi

Python3 如何在 4 个单词后向数组中的每个元素添加
标签?

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:03 25 4
gpt4 key购买 nike

我有一个数组 ElementA

ElementA=["Hello my name is Karen", "Andrew here with you and nice to meet you", "Hi, Sharon here"]

我希望为数组生成一个输出,它将返回如下:

OutputElementA= ["Hello my name is <br> Karen", "Andrew here with you <br> and nice to meet <br> you", "Hi, Sharon here"]

有人知道如何在每个元素的 4 个单词后将
标记添加到数组中的元素吗?

最佳答案

应该这样做。

ElementA=["Hello my name is Karen", "Andrew here with you and nice to meet you", "Hi, Sharon here"]
OutputElementA = []

for elem in ElementA:
#Split string into words
elem_list = elem.split(' ')
out_elem_str = ''
i=0
elem_item = []
while i < len(elem_list):
elem_item = elem_list[i:i+4]
if len(elem_item) == 4:
out_str = ' '.join(elem_item)+' <br> '
out_elem_str += out_str
i+=4
out_elem_str += ' '.join(elem_item)
OutputElementA.append(out_elem_str)

print(OutputElementA)

输出是

['Hello my name is <br> Karen', 
'Andrew here with you <br> and nice to meet <br> you',
'Hi, Sharon here']

关于Python3 如何在 4 个单词后向数组中的每个元素添加 <br> 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55506535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com