
题目让我们计数这道题第一反应是for循环重复就i1代码如下def number_of_occurrences(element, sample): total 0 for i in sample: if element i: total 1 return total遍历sample中元素计数。很C的写法。Python内置的count方法轻松也能达成代码如下def number_of_occurrences(element, sample): return sample.count(element).理解为from——from sample 统计 element 出现的次数。