import string from collections import defaultdict text = '''S/m,Text\n1,What a happy day.\n2,Happy new year\n3,Holy Cow''' wordcheck = defaultdict(list) # Instead of using the string, you can also uncomment line 8 and comment line 9 #for line in open('infile.txt'): for line in text.split('\n'): serial,_,comment = line.partition(',') if serial.isdigit(): # Cheatline to process comments (remove punctuations, lower case) comment = "".join([i.lower() for i in comment if i not in string.punctuation]) for word in comment.split(): wordcheck[word].append(serial) for i in wordcheck: print i+","+",".join(wordcheck[i]) print "#######################################" wordcheck = defaultdict(list) for line in text.split('\n'): serial,_,comment = line.partition(',') if serial.isdigit(): # Cheatline to process comments (remove punctuations, lower case) comment = "".join([i.lower() for i in comment if i not in string.punctuation]) for word in comment.split(): wordcheck[serial].append(word) for i in wordcheck: for j in wordcheck[i]: print j+","+i
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question