使用numpy的另一种解决方案是: 最初的回答 np.count_nonzero(np.diff(s)) 一个原生的Python解决方案是 sum(s[i - 1] != s[i] for i in range(1, len(s)))
我会选择这个,如果有错请纠正..!:) # This will append 1 in list if change occurs c = [1 for i,x in enumerate(s[:-1]) if x!= s[i+1] ] # Printing the length of list which is ultimately the total no. of change print(len(c))