John John - 1 year ago 108
Python Question

Program outputs 1131 not 1121

I have ran into a logic error that I cannot solve and would like some assistance towards it.
Here's my code

Mysentence = MySentence
MysentenceList = Mysentence.split()
List = []

for k in MysentenceList:
position = MysentenceList.index(k)
position = position + 1
position = str(position)

Number = ",".join(TheList)
Number = "\n" + Number
fileName = input("File Name: ")
fileName = fileName + ".txt"

Basically cannot handle unique words

Answer Source

If you want to assign each word a "unique id", you'd have to manage this in another data structure. Another list would do the trick:

UniqueWords = []
for k in ThesentenceList:
    if k in UniqueWords:
        position = UniqueWords.index(k)
        position = len(UniqueWords)
    position = position + 1
    position = str(position)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download