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

Shi Jie Tio

我有一个数组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个单词后向数组中的元素添加标签的想法

Devesh库玛·辛格

这应该做。

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']

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章