449,383 Members | 1,806 Online
Need help? Post your question and get tips & solutions from a community of 449,383 IT Pros & Developers. It's quick & easy.

# Function that is equivalent to the built-in list method count

 P: 4 here is sample for my_count(list,obj) Expand|Select|Wrap|Line Numbers def my_count(my_list,my_obj):     """ This function is equivalent to the built-in list function "count". """     if not(my_obj in my_list) :         return 0     else:         count_int=0         for current_obj in my_list:             if current_obj==my_obj:                 count_int=count_int+1         return count_int   def main():     test_list=['dog',1,'cat',1,3,'dog',4,['dog',3],1]     # Test my_count and compare it to built in list function count       # Test 1     print ("Built-in count function returned : ",test_list.count('dog'))     print ("my_count function returned : ", my_count(test_list,'dog'))       # Test 2     print ("Built-in count function returned : ",test_list.count(1))     print ("my_count function returned : ",my_count(test_list,1))       # Test 3     print ("Built-in count function returned : ",test_list.count('bird'))     print ("my_count function returned : ",my_count(test_list,'bird'))   if __name__ == "__main__":  # if the function is the main function then call the main()     main() Sep 24 '14 #1