My only comment is that this considers spaces and punctuation (like parentheses, brackets, etc.), too, which I assume you don't want seeing as how that has little to do with natural languages.  My suggestion would be to remove the any punctuation or whitespace keys from the dictionary after you've built the dictionary.  Unless, of course, you're interested in what letter words start with, in which case you could somehow merge them into a single key?
<br><br>Cheers,<br>Orri<br><br><div><span class="gmail_quote">On 4/4/06, <b class="gmail_sendername">Michael Broe</b> &lt;<a href="mailto:mbroe@columbus.rr.com">mbroe@columbus.rr.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Well coming up with this has made me really love Python. I worked on<br>this with my online pythonpenpal Kyle, and here is what we came up<br>with. Thanks to all for input so far.<br><br>My first idea was to use a C-type indexing for-loop, to grab a two-
<br>element sequence [i, i+1]:<br><br>dict = {}<br>for i in range(len(t) - 1):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not dict.has_key(t[i]):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict[t[i]] = {}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not dict[t[i]].has_key(t[i+1]):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict[t[i]][t[i+1]] = 1
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict[t[i]][t[i+1]] += 1<br><br>Which works, but. Kyle had an alternative take, with no indexing, and<br>after we worked on this strategy it seemed very Pythonesque, and ran<br>almost twice as fast.
<br><br>----<br><br>#!/usr/local/bin/python<br><br>import sys<br>file = open(sys.argv[1], 'rb').read()<br><br># We imagine a 2-byte 'window' moving over the text from left to right<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+-------+<br># L&nbsp;&nbsp;o&nbsp;&nbsp;n&nbsp;&nbsp;| d&nbsp;&nbsp; o |&nbsp;&nbsp;n&nbsp;&nbsp;.&nbsp;&nbsp;M&nbsp;&nbsp;i&nbsp;&nbsp;c&nbsp;&nbsp;h&nbsp;&nbsp;a&nbsp;&nbsp;e&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;a&nbsp;&nbsp;s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;&nbsp;e
<br>r&nbsp;&nbsp;m ...<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+-------+<br>#<br># At any given point, we call the leftmost byte visible in the window<br>'L', and the<br># rightmost byte 'R'.<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+-----------+<br># L&nbsp;&nbsp;o&nbsp;&nbsp;n&nbsp;&nbsp;| L=d&nbsp;&nbsp; R=o |&nbsp;&nbsp;n&nbsp;&nbsp;.&nbsp;&nbsp;M&nbsp;&nbsp;i&nbsp;&nbsp;c&nbsp;&nbsp;h&nbsp;&nbsp;a&nbsp;&nbsp;e&nbsp;&nbsp;l&nbsp;&nbsp;m&nbsp;&nbsp;a&nbsp;&nbsp;s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t
<br>e&nbsp;&nbsp;r&nbsp;&nbsp;m ...<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+-----------+<br>#<br># When the program begins, the first byte is preloaded into L, and we<br>position R<br># at the second byte of the file.<br>#<br><br>dict = {}<br><br>L = file[0]<br>for R in file[1:]:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# move right edge of window across the file
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not L in dict:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict[L] = {}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not R in dict[L]:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict[L][R] = 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict[L][R] += 1<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L = R&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # move character in R over to L
<br><br># that's it. here's a printout strategy:<br><br>for entry in dict:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print entry, ':', sum(dict[entry].values())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print dict[entry]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print<br><br>----<br><br><br><br><br>_______________________________________________
<br>Tutor maillist&nbsp;&nbsp;-&nbsp;&nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br><br clear="all">
<br>-- <br>Email: singingxduck AT gmail DOT com<br>AIM: singingxduck<br>Programming Python for the fun of it.