I'm currently writing an anagram program in java that uses a hashmap> to store the words. The dictionary text file I have is in the format:
eelrss lesser
eelrssst restless
eelrsst tressel
eelrsstvy sylvester
eelrst lester
eelrstt letters
eelrstt settler
eelrstt trestle
The letters of each word have been put in alphabetical order next to their word and the whole file sorted in alphabetical order using the keys.What I am trying to do is format it using Python so that duplicate words are on the same line, e.g. in the above example, eelrstt letters eelrstt settler
would become eelrstt letter settler trestle
The code I am using to try and convert this is:
f = open('d13.txt')
lines = f.readlines()
duped = open('checked.txt', 'w')
for i in range(len(lines)):
line1 = lines[i].split(' ')
line2 = lines[i+1].split(' ')
if line1[0] != 'nm':
if line1[0] == line2[0]:
line3 = line1 + line2[1:]
line2[0] = 'nm'
else:
line3 = line1
line4 = ' '.join(line3)
duped.write(line4)
However this produces a mess that only picks up on the next line and leaves duplicates in.
eelrss lesser
eelrssst restless
eelrsst tressel
eelrsstvy sylvester
eelrst lester
eelrstt letters
settler
eelrstt settler
trestle
eelrstt trestle
Can anyone please help?