help how to sort a list in order of 'n' in python without using inbuilt functions??

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat May 25 01:57:38 EDT 2013


On Fri, 24 May 2013 22:39:06 -0700, lokeshkoppaka wrote:

> On Saturday, May 25, 2013 10:54:01 AM UTC+5:30, Chris Angelico wrote:

>> In that case, you're not really ordering them, you're counting them.
>> Look at the collections module; you can very easily figure out how
>> many of each there are, and then reconstruct the list afterward.
> 
> but i need to do it with out using builtin functions


How would you, an intelligent human being count them?

Describe how you would count them in English, or whatever your native 
language is.

"First I would start a tally for the number of zeroes, tally = 0. Then I 
look at each item in turn, and if it is 0, I add one to the tally. When I 
get to the end, I the number of zeroes is equal to the tally.

Then I do the same for the number of ones, and the number of twos."

Now turn that into Python code. 

tally = 0
for item in list_of_items:
    if item == 0:
        tally = tally + 1

print "The number of zeroes equals", tally



-- 
Steven



More information about the Python-list mailing list