<br><br><div class="gmail_quote">On Wed, Feb 16, 2011 at 4:27 AM, Chris Rebert <span dir="ltr"><<a href="mailto:clp2@rebertia.com">clp2@rebertia.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Wed, Feb 16, 2011 at 1:17 AM, Gilles Ganault <<a href="mailto:nospam@nospam.com">nospam@nospam.com</a>> wrote:<br>
> Hello,<br>
><br>
> For a game, I need to go through a wordlist, and for each word,<br>
> compute its value, ie. a=1, b=2, etc.<br>
><br>
> So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.<br>
><br>
> Before I write the obvious While loop to go through each line in the<br>
> input text file, I was wondering if Python didn't already have some<br>
> function to perform this type of computation.<br>
<br>
</div>A = ord('a') - 1<br>
for line in your_file:<br>
    word = line.strip().lower()<br>
    score = sum(ord(letter)-A for letter in word)<br><br></blockquote><div><br></div><div>Or a one-liner (import not included):</div><div><br></div><div>In [26]: import numpy as np</div><div><br></div><div>In [27]: (np.frombuffer(buffer('NewYork'.lower()), dtype='uint8') - 96).sum()</div>
<div>Out[27]: 111</div><div><br></div><div> </div></div><br>