[Tutor] words and lengths

Mike Hansen Mike.Hansen at atmel.com
Wed May 16 18:06:10 CEST 2007


 

> -----Original Message-----
> From: tutor-bounces at python.org 
> [mailto:tutor-bounces at python.org] On Behalf Of Emad Nawfal
> Sent: Wednesday, May 16, 2007 9:40 AM
> To: Tutor at python.org
> Subject: [Tutor] words and lengths
> 
> Hi All,
> I'm beginning to learn Python for linguistic research using 
> the Natural Language Toolkit. I want to write a small piece 
> of code to count lengths of words and print them a long with 
> the words. I've tried this but it does not work. What's wrong 
> with it, and how can I fix it? 
>  
> 
> phrase2 = ['colorless', 'green', 'ideas', 'sleep', 'furiously']
> lengths = {}
> for word in phrase2:
>     lengths = lengths[word, len(word)] 
> 
> print lengths
> 
> 
> Thank you in anticipation

Instead of
lengths = lengths[word, len(word)]

I think it should be
lengths[word] = len(word) # dictionary[key] = value

Mike


More information about the Tutor mailing list