簡體   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)

我試過了,但沒有用我該怎么做

執行循環時不建議刪除列表中的項目。 最好創建一個新列表並將 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