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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pythonlearner
    New Member
    • Sep 2014
    • 4

    #1

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

    here is sample for my_count(list,o bj)
    Code:
    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()
    Last edited by bvdet; Sep 24 '14, 01:11 PM. Reason: Please use code tags when posting code [code].......[/code] - Also clarify title
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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
    Code:
    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))

    Comment

    • pythonlearner
      New Member
      • Sep 2014
      • 4

      #3
      my bad my question wasn't completely typed over here.
      so here's my question:-
      Write functions to implement(simul ate) 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!!!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        pythonlearner,

        We are not hear to write code for you. Here's a hint for my_append.
        Code:
        >>> 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.

        Comment

        Working...