counting occurrence of consecutive elements in a list (python) -


given a list

  a = ['g', 'd', 'r I want to count as GDR incident for example, so I will apply some functions. Returned  a.function ('g', 'd', 'r') and sent me  2  (since gdr was 2 times).  

If they are the only string in the list, And its count method:

  & gt; & Gt; & Gt; A = ['g', 'd', 'r', 'x', 's', 'g', 'd', 'r']> gt; & Gt; & Gt; For example, if you have a mix of strings and numbers, you can use the  map (str, a). And the method will still work before joining:  
  & gt; & Gt; & Gt; a = [1,13,4,2,1,13,4] & gt; & Gt; & Gt; ''. () () (), () () () () () (), () () () () (), 

  def count_pattern (lst, * pattern): return '' .join (L) .count (''. Join (pattern))   

and will be able to call it: count_pattern (a, 'g', 'd', ' R ') or count_pattern (a,' gdr ') or even count_pattern (a,' gd ',' r ') .

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -