How to make a function that counts how many times each element is equal to 2 elements to its right

后端 未结 3 827
一整个雨季
一整个雨季 2021-01-23 11:15

I know i need to use a list comprehension but for the life of me I cannot figure out what would be the correct way to denote this. An example of this running right would be for

3条回答
  •  离开以前
    2021-01-23 11:24

    The list comprehension gives the letters that have the same letter two places to the right. We simply take the length of the resulting list:

    s = "evening"
    ans = len([x for x in xrange(len(s)-2) if s[x] == s[x+2]])
    print ans
    

提交回复
热议问题