[英]Find number of occurrences of groups of '-' sign in list in Python
這是我在這里的第一個問題。
我想知道如何從這個:
[1,1,2,'-','-',3,3,'-','-','-','-','-',5,5,'-','-','-']
查找此形狀中出現的'-'
符號: [2,5,3]
您可以使用itertools groupby
:
from itertools import groupby
l = [1, 1, 2, '-', '-', 3, 3, '-', '-', '-', '-', '-', 5, 5, '-', '-', '-']
result = [len(list(v)) for k, v in groupby(l) if k == '-']
[2, 5, 3]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.