【发布时间】:2016-04-23 13:51:11
【问题描述】:
我需要开发一个程序来接收用户输入的字符串,例如:
'to be or not to be'.
然后将字符串分成单独的单词:
'to', 'be', 'or', 'not', 'to', 'be'
然后将单词放入列表中:
['to', 'be', 'or', 'not', 'to', 'be']
但是,如果一个词被重复,则只计算该词的第一次使用并替换为它的位置编号。因此,例如,to be or not to be 中的 to 都将被计为 1。这是因为单词 to 重复出现,因此 to 的第一次出现占了这两个单词。将单词替换为其位置编号后,应使用这些编号重新创建句子。所以to be or not to be应该变成:
1, 2, 3, 4, 1, 2
单词和数字列表应保存为单独的文件或单个文件。 到目前为止,这就是我所拥有的:
UserSentence = input('Enter your chosen sentence: ') #this is where the user inputs their sentence
UserSentence = UserSentence.split()#.split()takes the inputted string and breaks it down into individual words...
#... and turns it into a list
List1 = UserSentence
List2 = UserSentence
【问题讨论】:
-
在我看来,这不足以证明提出这样的要求。
-
我已经弄清楚如何从列表中删除重复的单词
-
你需要证明你已经努力解决这个问题......
-
我已经这样做了好几天了
-
从阅读文档开始怎么样?比如,
list上有哪些方法可用?
标签: python python-3.x