[Tutor] How to improve
Alan Gauld
alan.gauld at btinternet.com
Thu Jul 12 16:28:39 CEST 2007
"taserian" <taserian at gmail.com> wrote
> I'd like to see how I could improve the program below for this, and
> make it
> more Python-ish.
>
> = = = = START
>
> def decompose(longword):
> word1 = longword[0::2]
> word2 = longword[1::2]
> return (word1, word2)
This seems OK to me.
> wordlengthlist = [None] * 31
> for i in range(0,len(wordlengthlist)):
> wordlengthlist[i] = []
But this could be done using a list comprehension:
wordlengthlist = [[] for i in range(31))
> results = []
>
> for lineread in open("word.list"):
> for word in lineread.split():
> if len(word)<31:
> wordlengthlist[len(word)].append(word)
>
> outfile = open('InterleaveEnglishYAWL.txt', 'w')
>
> for x in range(4, 31):
> print x
> for i in wordlengthlist[x]:
> twowords = decompose(i)
> word1 = twowords[0]
> word2 = twowords[1]
the three lines above can be done as
word1,word2 = decompose(i)
> if word1 in wordlengthlist[len(word1)] and word2 in
> wordlengthlist[len(word2)]:
> outfile.write("(" + word1 + ", " + word2 + ", " + i +
> ")\n")
> print (word1, word2, i)
>
> outfile.close();
>
> = = = = END
Those are the only significant changes I wouldmake.
Alan G.
More information about the Tutor
mailing list