【发布时间】:2016-12-12 20:03:30
【问题描述】:
sentence = input("Please enter a sentence:")
words = sentence.split()
position= [0]
for count, i in enumerate(words):
if words.count(i) <2:
position.append(max(position) +1)
else:
position.append(words.index(i) +1)
position.remove(0)
print (position)
words = str(words)
position = str(position)
file = open("list_and_positions_of_words.txt","w")
file.write (words +'\n')
file.write(position)
file.close()
例子:你好,你今天在做什么?
我的程序会将单词重新创建到位置,因此文本文件上的输出将是 ['Hello,' 'what', 'are', 'you', 'doing', 'today?'] [1, 2,3,4,5,6]
我现在需要做的是获取这个文件,打开它并阅读它,然后打印包含大写字母和标点符号的原始句子(这不会作为列表)。
有没有一种方法可以删除方括号和逗号,但不能删除句子中的逗号,只是将单词分隔为列表的逗号。
谢谢
【问题讨论】:
-
这是一个非常重要的概念。列表不仅仅是一个包含额外字符的字符串。这是一种完全不同的数据存储方式,并且在计算机内存中的记录方式也不同。要连接字符串列表,请执行
''.join(list_of_strings) -
您的
position列表到底应该代表什么?肯定不会记录每个单词的位置.....