我创建了这个程序,要求将位置列表输入到文件中,而不是将其保存为列表,并将其另存为另一个文件,用户必须输入每个位置具有的单词列表。例如,如果我输入:从位置创建句子时发生逻辑错误
1 2 3 4 5 1 2 3 4 5
和
this is a repeated sentence
的outpur应该
this is a repeated sentence this is a repeated sentence
然而,当我运行我的代码,我得到的位置的列表,而不是
1 2 3 4 5 1 2 3 4 5
这里是我的代码:
import subprocess
process_position = subprocess.Popen(["notepad","list_of_numbers.txt"])
process_position.wait()
positions = []
with open("list_of_numbers.txt","r") as f:
positions = f.read().split()
process_words = subprocess.Popen(["notepad","list_of_words.txt"])
process_words.wait()
sentence = ""
with open("list_of_numbers.txt","r") as s:
sentence= s.read().split()
mapping = {}
for (position, word) in zip(positions, sentence):
mapping[position] = word
output = [mapping[position] for position in positions]
print(' '.join(output))
嘛'positions'确实含有** **的字符串,而不是** **整数... –
使用'映射[INT(位置)] = word' ... –
我试过,但我得到一个语法错误 –