RE: Help with python

  • From: "Ken Perry" <whistler@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sat, 24 Oct 2009 23:55:13 -0400


Well the key is your instructor said it doesn't have to be in order.  The
structure I would use is a dictionary.  I am not sure there is a cleaner way
to do this but I can tell you adding to a dictionary in python is one very
fast function so the following is how I would do it.  Note the try except
that is there for the first time you add a word to a dictionary that could
also be handled by an if statement that checked to see if there was already
key in the list and adding it if there wasn't like I do in the except
statement but I felt that that would make the code to long.

list=['the','word','the','is','the','mode','of','the','list']
dict={}
for u in list:
    try: dict[u]+=1
    except: dict[u]=1
    
print "The unique list is %s\n"  % str(dict.keys())

word=''
count=0
for k, v in dict.iteritems():
    if v>count:
        word=k
        count=v
         
         
print "the string witht he most is %s and there are %d of them\n" %
(word,count)

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of edward
Sent: Saturday, October 24, 2009 6:21 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: Help with python

Hello 

Thank you for the email.

Yes I think I was reading the problem wrong but I did write the code and it
works fine.  Problem two is a problem however.
edward 

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Martin Slack
Sent: Saturday, October 24, 2009 5:57 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Help with python

I couldn't remember what a mode was so I looked at:

http://www.mathgoodies.com/lessons/vol8/mode.html

It would seem that both problems have to do with counting the numbers of
identical elements in a set or list.  Can you write a programme to do that
as a start?

  Martin

----- Original Message -----
From: "edward" <personal.edward@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Saturday, October 24, 2009 10:02 PM
Subject: Help with python


> hello all,
>
> I have 2 questions on my homework assignment that i cannot figure out.
>
> 1. write a function that will return the mode for a data set.  A data set 
> is
> a variable that contains more then one word.
> 2. Write a function called remove_duplicates that takes a list and returns

> a
> new list with only the unique elements from the original. Hint: they don't
> have to be in the same order.
>
> any help will be greatly appreciated.
> Edward
>
> 

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: