usr bin env python -coding utf8 import string file open testfile text

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
#-*-coding:utf8
import string
file=open("testfile")
text=file.read()
word_list=text.split()
unikal=set(word_list)
result=[]
for word in unikal:
info={"word" : word,
"quantity":string.count(text,word)}
result.append(info)
print result
print "#########################################################"
result=[]
for word in unikal:
info={}
count=0
for _word in word_list:
if _word == word:
count=count+1
info["word"]=word
info["quantity"]=count
result.append(info)
print result
print "#########################################################"
result=[{"word" : word , "quantity" : string.count(text,word)} for word in unikal ]
print result
print "#########################################################"
result=[{"word" : word , "quantity" : len(filter(lambda x:x==word, text.split() ))} for word in unikal ]
print result
#