424,963 Members | 1,049 Online Need help? Post your question and get tips & solutions from a community of 424,963 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
3 Replies

 Expert 100+ P: 613 You have not asked a question so there is nothing we can do. Since you are still using a main() function I assume you are a student of some kind, so tell us what this assignment is supposed to do. Note that the purpose of the statement if __name__ == "__main__": is that it is executed if the program is run from the command line or an IDE, etc.. If the program is imported into another program and run from that program then the statements following it will not be executed. Note also that Python's builtin count or a version of the count code could be used instead of if my_obj in my_list: as they do the same thing. Edit: I assume you want to count the number of times a certain item appears in the list, which would be Expand|Select|Wrap|Line Numbers def my_count(my_list,my_obj):     """ This function is equivalent to the built-in list function "count". """     count = 0     for current_obj in my_list:         if current_obj==my_obj:             count += 1     return count   if __name__ == "__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     for lit in ["dog", 1, "bird"]:         print("----->", lit)         print ("Built-in count function returned : ",                test_list.count(lit))         print ("my_count function returned       : ",                 my_count(test_list, lit))  Sep 24 '14 #2

 P: 4 my bad my question wasn't completely typed over here. so here's my question:- Write functions to implement(simulate) built in Python functions for the following: my_append(list,z) my_extend(list,z) my_insert(list,i,y) my_len(list) my_pop(list,[x]) my_reverse(list) please help.. thanks in advance!!! Sep 24 '14 #3

 Expert Mod 2.5K+ P: 2,851 pythonlearner, We are not hear to write code for you. Here's a hint for my_append. Expand|Select|Wrap|Line Numbers >>> some_list = [1,2,3,4,5] >>> x = 6 >>> some_list1 = some_list + list([x]) >>> some_list1   [1, 2, 3, 4, 5, 6] >>>  Something similar can be done for my_extend. Post back if you need help with your code. Sep 25 '14 #4 