简体   繁体   English

如何从 for 循环 python 的列表中删除项目

[英]How can i delete items from list in for loop python

text = '<span>geldi <span data-jid="@c.us" data-display="Gerçek Ege" class="matched-mention i0jNr selectable-text select-all copyable-text" data-plain-text="@Gerçek Ege" data-app-text-template="​905386782644@c.us​"><span class="at-symbol">@</span><span dir="ltr">Gerçek Ege</span></span></span>'
text1 = text.replace("<span>"," "), text.replace("<span"," "), text.replace(">"," "), text.replace("</span>"," ")
text1= ''.join(text1)
tex2= text1.rsplit(" ")
for i in tex2:
    if "data-jid="or"data-display="or"class="or"i0jNr"or"selectable-text"or"select-all"or"copyable-text"or"data-plain-text="or"data-app-text-template="or"dir="or"</span>" in i:
        tex2.remove(i)
print(text1)
print(tex2)

i tryed this but it didnt worked how can i do this我试过了,但没有用我该怎么做

It is not advisable to remove item in a list when doing a loop.执行循环时不建议删除列表中的项目。 It is better to create a new list and append items into that new list (tex3).最好创建一个新列表并将 append 项放入该新列表 (tex3)。

text = '<span>geldi <span data-jid="@c.us" data-display="Gerçek Ege" class="matched-mention i0jNr selectable-text select-all copyable-text" data-plain-text="@Gerçek Ege" data-app-text-template="​905386782644@c.us​"><span class="at-symbol">@</span><span dir="ltr">Gerçek Ege</span></span></span>'
text1 = text.replace("<span>"," "), text.replace("<span"," "), text.replace(">"," "), text.replace("</span>"," ")
text1= ''.join(text1)
tex2= text1.rsplit(" ")
tex3=list()
for i in tex2:
    if "data-jid="or"data-display="or"class="or"i0jNr"or"selectable-text"or"select-all"or"copyable-text"or"data-plain-text="or"data-app-text-template="or"dir="or"</span>" in i:
        continue
    else:
        tex3.append(i)
print(text1)
print(tex3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM