Ron Nixon wrote:
Like this?I know that you can do this to get a count of home many times a word appears in a filef = open('text.txt').read() print f.count('word') Other than using a several print statments to look for seperate words like this, is there a way to do it so that I get a individual count of each word: word1 xxx word2 xxx words xxx etc. A 14 AND 1 Abantes 3 Abarbarea 1 Abas 1 Abians 1 Ablerus 1 About 2 Abydos 3 Acamas 11 Accept 2 Acessamenus 1 Achaea 1 Achaean 34 Achaeans 540 Achelous 2 Achilles 423 Acrisius 1 Actaea 1 Actor 8 Adamas 5 Admetus 4 Adrastus 2 Adresteia 1 Adrestus 8 Aeacus 20 Aegae 2 Aegaeon 1 Aegeus 1 Aegialeia 1 Aegialus 1 Aegilips 1 Aegina 1 Aegium 1 Aeneas 86 Aenus 1 Aeolus 1 Aepea 2 Aepytus 1 Aesculapius 7 Aesepus 2 Aesopus 4 Aesyetes 2 Aesyme 1 Aesymnus 1 ... wronged 2 wronging 1 wrongs 1 wroth 1 wrought 24 wrung 1 yard 3 yarded 1 yards 2 yawned 1 ye 3 yea 1 year 13 yearling 2 yearned 4 yearning 2 years 15 yellow 5 yesterday 5 yet 160 yield 10 yielded 3 yielding 3 yieldit 1 yoke 24 yoked 11 yokes 1 yokestraps 1 yolking 1 yonder 3 you 1712 young 44 younger 9 youngest 6 your 592 yourelf 1 yours 7 yourself 60 yourselves 17 youselves 1 youth 17 youths 18 zeal 2 I ran the following script on "The Iliad": #!/usr/bin/env python import string text = open('iliad.txt', 'r').read() for punct in string.punctuation: text = text.replace(punct, ' ') words = text.split() word_dict = {} for word in words: word_dict[word] = word_dict.get(word, 0) + 1 word_list = word_dict.keys() word_list.sort() for word in word_list: print "%-25s%d" % (word, word_dict[word]) Jeremy Jones |
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor