List problem

Jon Cosby jcosby at mindspring.com
Sat Mar 30 08:34:52 EST 2002


"Terry Reedy" <tejarex at yahoo.com> wrote in message
news:xU6p8.28161$VJ1.2411615 at bin3.nnrp.aus1.giganews.com...
>
> "Jon Cosby" <jcosby at mindspring.com> wrote in message
> news:a82mlc$ja1$1 at slb6.atl.mindspring.net...
> > Can anyone se why this is coming up empty in the "cols" list? I've
> tried
> > something similar in the interpreter, and everything here looks
> right.
>
> What happens if you paste exactly this text into the interpreter (on
> PC, turn off fast pasting)?  (Answer: syntax errors, see below.)
>
> ######################################################################
> ##
> > # WordSquare
> > # Build word squares from initial word
>
> > # Word dictionary
> > dict = "c:\data\dict.txt"
>
> As I said elsewhere, use forward slashes / to be safe, concise, and
> more portable.
>
> > firstword = input("Enter first word (enclosed in quotes): ")
>
> firstword = raw_input("Enter first word (do not enclose in quotes): ")
> is safer and easier for user.
>
> > lword = len(firstword)
> > words = []
> > cols = []
> > rows = []
> >
> > f = open(dict, "r")
> > for line in f.readlines():
> >  if len(line[:-1]) == lword:
> >   words.append(line[:-1])
> #  add 'word = line[:-1]' before if and sub in nect too two lines
> slightly faster
>
> > f.close()
> Need blank line to run interactively
>
> If words were sorted by len, you could read in only those of proper
> length
>
> > for i in range(lword):
> >  cols.append([])
> >  rows.append([])
>
> > rows[0].append(firstword)
> >
> > # Generate an array of words with matching first letters
> > for i in range(lword):
> >  for word in words:
> >    if word[0] == firstword[i]: # Matches not found
> >    cols[i].append(word)
>
> As written, without indent of 'cols...' after 'if', this has a syntax
> error.
> This suggests that this is not exactly the file that produced the
> output below.
>
> If words of given length were sorted, you could grab just those
> needed.
>
> > print len(words)
> > print cols
> >
> >
> ######################################################################
> >
> > C:\Python21>python projects\wordsquare.py
> > Enter first word (enclosed in quotes): "hello"
> > 4220
> > [[], [], [], [], []]
>
> Try print words[0:10] to see what actually iterating through.
>
> # I pasted following into interpreter and got output marked by #
>
> firstword = "cow" # cow, ore, wee/wet make square
> lword = len(firstword)
> words = ['act', 'art', 'boo', 'cat', 'cad', 'coo', 'cow', 'oft',
> 'opt',
> 'ore', 'wee', 'wet']
> cols = []
> rows = []
>
> # delete readin from file stuff
>
> for i in range(lword):
>  cols.append([])
>  rows.append([])
>
> rows[0].append(firstword)
>
> # Generate an array of words with matching first letters
> for i in range(lword):
>  for word in words:
>    if word[0] == firstword[i]: # Matches not found
>      cols[i].append(word)
>
> print len(words)
> #12
> print cols
> #[['cat', 'cad', 'coo', 'cow'], ['oft', 'opt', 'ore'], ['wee', 'wet']]
>
> so this much of program works.
>

It matches these words. Appending the dictionary to these, it finds them
again, but only these words.


Jon





More information about the Python-list mailing list