我想为单词分配数字(从一开始)并创建两个单词,其中一个单词,其中一个数字。然后创建一个文件并在其中存储两个列表。到目前为止,我有:
sentence = input('Please enter a sentence: ')
list_of_words = sentence.split()
words_with_numbers = enumerate(list_of_words, start=1)
答案 0 :(得分:0)
只需int( input("First Operator: "))
个文件并写入
open
然后
In [1]: sentence = input('Please enter a sentence: ')
Please enter a sentence: Hello World
In [2]: with open('output.txt', 'w') as f:
...: for i, word in enumerate(sentence.split(), start=1):
...: f.write("{} {}\n".format(i, word))
...:
答案 1 :(得分:0)
您可以尝试(如果这是您想要的):
sentence = input('Please enter a sentence: ')
list_of_words = sentence.split()
words_with_numbers = enumerate(list_of_words, start=1)
filename = 'yourfilename.txt'
with open(filename, 'w+') as file:
file.write(str(list_of_words) + '\n' + str(words_with_numbers) + '\n')