-1
sentence = raw_input("Enter a sentence: ")
sentence = sentence.lower().split()
uniquewords = []
for word in sentence:
    if word not in uniquewords:
        uniquewords.append(word)

positions = [uniquewords.index(word) for word in sentence]

recreated = " ".join([uniquewords[word] for word in positions])

positions = [x+1 for x in positions]
print uniquewords
print positions
print recreated

file = open('task2file1.txt', 'w')
file.write('\n'.join(uniquewords))
file.close()

file = open('task2file2.txt', 'w')
file.write('\n'.join(positions))
file.close()

This is the code I have so far and everything works except saving the positions into a textfile, the error message I get is

"file.write('\n'.join(positions))
TypeError: sequence item 0: expected string, int found"
Selcuk
  • 57,004
  • 12
  • 102
  • 110

2 Answers2

3

The .join() method can only join string lists. You must convert the ints in your position list to strings:

file.write('\n'.join(str(p) for p in positions))

or

file.write('\n'.join(map(str, positions)))
Selcuk
  • 57,004
  • 12
  • 102
  • 110
2

Convert positions to a list of strings.

file.write('\n'.join(str(p) for p in positions))
Hackaholic
  • 19,069
  • 5
  • 54
  • 72