473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
3 1462
dwblas
626 Expert 512MB
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
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
2,851 Expert Mod 2GB
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

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

Similar topics

3
by: Brad Tilley | last post by:
Can a function return a list?
2
by: FAN | last post by:
I want to define some function in python script dynamicly and call them later, but I get some problem. I have tried the following: ################################## # code...
3
by: Steve | last post by:
Is there equivalent of sscanf function in .NET? I need to parse string with network MAC address (For example like 11-22-33-44-55-66) and extract each segment into array of bytes. Can anyone...
9
by: ogerchikov | last post by:
I have 2 classes, A, B and B is a child of A. and I have a function which processes a list of A. void func(list<A> alist) { // processing list of A } The problem is func() won't able to...
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
2
by: Adam Kubica | last post by:
Hellou. Anybody know about code that work equivalent to gzinflate() function used in PHP? I search via google but I don't found anything sensible :-(
3
by: er | last post by:
hi, class Gen{ public: Gen(double (*fun_)()):fun(fun_){}; private: double (*fun)(); }; class Op{ public:
1
by: jeddiki | last post by:
Hi, I am confused about whether I should be using just the file() function or both fopen() function and the file() function. I have a file that is a list of words in a text fle. Now I want...
0
by: slenish | last post by:
Hello all, I am wondering if there is a way to expand on the not in list function? I have 5 combo boxes that pull information from a table on the back end. But I want to make it so if the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.