Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

python - Counting unique words

Question:

Devise an algorithm and write the Python code to count the number of unique words in a given passage.

The paragraph may contain words with special characters such as !, ?, ., , , : and ; and digits are not permitted.

Special character must occur only at the end of a word in a passage that is Hello World! is valid but Hello !World or Hello Wor!ld is invalid.

No two special character occur together. Print Invalid input for such cases.

Count words without special characters. Counting must case insensitive. Print words in lower case and in sorted order.

My code:

import sys
from pprint import pprint`
import re
line=raw_input()
line.lower()
l=line.split(" ")
d=set(l)
count={}
for word in d:
    if word in count:
        count[word]+=1
    else:
        count[word]=1
pprint(count)

Expected Output:

{'are': 2, 'better': 1, 'dear': 2, 'how': 1, 'you': 2}

My Program Output:

{'Are': 1, 'How': 1, 'are': 1, 'better': 1, 'dear?': 1, 'you': 1}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to remove these 2 lines and add the line:

REMOVE:  
line.lower()   
l=line.split(" ")

ADD:
l = re.sub(r"s+[!?.,:@]+s+", r" ", s2.lower()).split(" ")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...