簡體   English   中英

從列表中刪除與條件匹配的第一個匹配項

[英]Remove first occurrence that matches criteria from a list

假設我有一個字符串列表:

first item
second item
# first commented item
third item
# second commented item

如何從列表中刪除以#開頭的第一個項目?

預期結果:

first item
second item
third item
# second commented item
>>> items = ["First", "Second", "# First", "Third", "# Second"]
>>> for e in items:
...     if e.startswith('#'):
...             items.remove(e)
...             break
... 
>>> items
['First', 'Second', 'Third', '# Second']
items = ["First", "Second", "# First", "Third", "# Second"]
for i in xrange(len(items)):
    if items[i][0] == '#':
        items.pop(i)
        break
print items

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM