Python searching patterns in lists of strings -
i have list of strings following in python:
ss = ['t', 'q', 't', 'd', 'q', 'd', 'd', 'q', 't', 'd']
is there anyway check how many ts directly followed d, in case, there 2 ts (second , last t) meet requirement.
i tried not working though, advice? thx!
if ['t','q'] in ss: print ("yes")
you iteratively checking if next character d
, implementing sort of counter. (probably) tersest, although maybe not best way.
ss = ['t', 'q', 't', 'd', 'q', 'd', 'd', 'q', 't', 'd'] print("".join(ss).count("td"))
result:
2
Comments
Post a Comment