[Tutor] Using dictionary to find the mode of a list

Alan Gauld alan.gauld at btinternet.com
Fri Oct 26 01:58:13 CEST 2007

"Conor Leister" <conorleister at gmail.com> wrote

> Here's the code I have that gathers the data and sorts it.

My first question is why are you writing a bubble sort
routine for your list? In almost every conceivable case
the built-in sort method will be faster.

> be able to get the mode of a using a dictionary

> i'm completely lost. this code works fine, i just need to add
> to it and i'm not great with dictionaries.

If it works fine why do you need to add to it?
And what do you find difficult with dictionaries?

I'm not clear what exactly the problem is with which
you want us to help you.

Alan Gauld
Author of the Learn to Program web site

> #gather data
> a=[]
> prompt='Enter numbers, blank line to finish:'
> inVal=raw_input(prompt)
> while inVal:
>    a.append(int(inVal))
>    inVal=raw_input(prompt)
> print 'The values you entered:'
> print a
> #Bubble Sort
> i=0
> while i<len(a):
>    j=1
>    while j<len(a)-i:
>        if a[j-1]>a[j]:
>            temp=a[j]
>            a[j]=a[j-1]
>            a[j-1]=temp
>        j=j+1
>    i=i+1
> print 'Values sorted: ' ,a


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list