On Wed, Feb 16, 2011 at 4:27 AM, Chris Rebert <c...@rebertia.com> wrote:

> On Wed, Feb 16, 2011 at 1:17 AM, Gilles Ganault <nos...@nospam.com> wrote:
> > Hello,
> >
> > For a game, I need to go through a wordlist, and for each word,
> > compute its value, ie. a=1, b=2, etc.
> >
> > So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.
> >
> > Before I write the obvious While loop to go through each line in the
> > input text file, I was wondering if Python didn't already have some
> > function to perform this type of computation.
>
> A = ord('a') - 1
> for line in your_file:
>    word = line.strip().lower()
>    score = sum(ord(letter)-A for letter in word)
>
>
Or a one-liner (import not included):

In [26]: import numpy as np

In [27]: (np.frombuffer(buffer('NewYork'.lower()), dtype='uint8') -
96).sum()
Out[27]: 111
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to