Splitting synset and removing duplicate words

63 Views Asked by At
Score      SynsetTerms
 1          soft#18 mild#3 balmy#2
 1          love#2 enjoy#3
0.625       love#1

From the input above, how can i achieve the output as below? I wish to create a file that has the word and their score by removing duplicate words, and splitting each word into a new row. Duplicated words with higher score will be selected instead.

Score      SynsetTerms
 1          soft
 1          mild
 1          balmy
 1          enjoy
 1          love 

note that the word 'love' with 0.625 score was removed, only the 'love' with score 1 is kept as it has higher score.

1

There are 1 best solutions below

0
On
import re
lst = []
dict = {}
i = 1
fhand = open('C:/Users/10648879/Documents/python_prog/data/test.csv', 'r')
for line in fhand:
    if i == 1:
    i = i + 1
continue
line = re.sub('#[0-9]*', '', line).strip()
line = re.split('\s+', line)
for counter in range(len(line)):
    if counter == 0:
    score = line[counter]
continue
if line[counter] in dict:
    if score > dict[line[counter]]:
    dict[line[counter]] = score
else :
    dict[line[counter]] = score
i = i + 1
print 'score' + ' ' + 'SynsetTerms'
for k, v in dict.items():
    print v, k