By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
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
  1. def my_count(my_list,my_obj):
  2.     """ This function is equivalent to the built-in list function "count". """
  3.     if not(my_obj in my_list) :
  4.         return 0
  5.     else:
  6.         count_int=0
  7.         for current_obj in my_list:
  8.             if current_obj==my_obj:
  9.                 count_int=count_int+1
  10.         return count_int
  11.  
  12. def main():
  13.     test_list=['dog',1,'cat',1,3,'dog',4,['dog',3],1]
  14.     # Test my_count and compare it to built in list function count
  15.  
  16.     # Test 1
  17.     print ("Built-in count function returned : ",test_list.count('dog'))
  18.     print ("my_count function returned : ", my_count(test_list,'dog'))
  19.  
  20.     # Test 2
  21.     print ("Built-in count function returned : ",test_list.count(1))
  22.     print ("my_count function returned : ",my_count(test_list,1))
  23.  
  24.     # Test 3
  25.     print ("Built-in count function returned : ",test_list.count('bird'))
  26.     print ("my_count function returned : ",my_count(test_list,'bird'))
  27.  
  28. if __name__ == "__main__":  # if the function is the main function then call the main()
  29.     main()
Sep 24 '14 #1
Share this Question
Share on Google+
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
  1. def my_count(my_list,my_obj):
  2.     """ This function is equivalent to the built-in list function "count". """
  3.     count = 0
  4.     for current_obj in my_list:
  5.         if current_obj==my_obj:
  6.             count += 1
  7.     return count
  8.  
  9. if __name__ == "__main__":
  10.     test_list=['dog',1,'cat',1,3,'dog',4,['dog',3],1]
  11.  
  12.     # Test my_count and compare it to built in list function count
  13.     for lit in ["dog", 1, "bird"]:
  14.         print("----->", lit)
  15.         print ("Built-in count function returned : ",
  16.                test_list.count(lit))
  17.         print ("my_count function returned       : ", 
  18.                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

bvdet
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
  1. >>> some_list = [1,2,3,4,5]
  2. >>> x = 6
  3. >>> some_list1 = some_list + list([x])
  4. >>> some_list1
  5.  
  6. [1, 2, 3, 4, 5, 6]
  7. >>> 
Something similar can be done for my_extend. Post back if you need help with your code.
Sep 25 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.