![](/img/trans.png)
[英]Python remove matches from list of list based on first and or second values
[英]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.