输出代码时为什么位置错误?

时间:2017-04-06 18:22:47

标签: python

myFile = open("task3.txt","r")
myList = myFile.readlines()
word = myList[0].split(' ')
position = [0]

for  count, i in enumerate(word):
    if word.count(i) < 2:
       position.append(count+1)
    else:
       position.append(word.index(i)+1)

position.remove(0)
print(position)
recreate= []
for count in position:
    recreate.append(word[count-1])
    print(recreate)
    with open ("test.txt","w") as file:
        file.write(" ".join(recreate))

这里我的代码应该将读取文件拆分为单词和位置,并使用它们在新文件中重新创建句子。这样做是正确的,但是当我打印位置时它们是错误的:

这是正确的位置:[1,2,3,4,5,5,4,3,6,7,8]

task3.txt =一,二,三,四,五,五,四,三,二和一。

这就是印刷品:[1,2,3,4,5,5,4,3,9,10,11]

test.txt =一,二,三,四,五,五,四,三,二和一。

谢谢。

1 个答案:

答案 0 :(得分:0)

您希望职位能够引用一些列表,其中所有单词都是唯一的,但您从未创建过这样的列表。您反而指的是您希望重复的原始列表。因此,当然数字会跳过跳过现有的副本。

我认为你打算制作一份新的独特令牌列表,如下所示:

data = "one, two, three, four, five, five, four, three, two and one."

tokens = []
position = []
for word in data.split(' '):
    if word in tokens:
        position.append(tokens.index(word))
    else:
        position.append(len(tokens))
        tokens.append(word)

print("Here are the unique words: ")
print(tokens)

def inc(n):
    return n+1
print("Here is a list of tokens in the input, where the indexes are changed to one-based indexing:")
print(map(inc, position))

recreate= []
for token_index in position:
    recreate.append(tokens[token_index])

print("Before:" + data)
print("After :" + " ".join(recreate))

输出:

Here are the unique tokens: 
['one,', 'two,', 'three,', 'four,', 'five,', 'two', 'and', 'one.']
Here is a list of tokens in the input, where the indexes are changed to one-based indexing:
[1, 2, 3, 4, 5, 5, 4, 3, 6, 7, 8]
Before:one, two, three, four, five, five, four, three, two and one.
After :one, two, three, four, five, five, four, three, two and one.