sentence = input("Please enter a sentence:")
words = sentence.split()
position= [0]
myList = []
[myList.append(x) for x in words if x not in myList]
a =(" ".join(myList))
print (a)
这段代码允许用户输入一个句子并删除重复的单词。当程序输出句子时,它输出如下句子:
例如,如果我的句子是'我喜欢python'我的程序会打印出来
I like python
到屏幕上
我需要帮助的是我希望程序输出:
['I', 'like', 'python']
请提出任何建议
答案 0 :(得分:2)
您只需要在程序中打印myList。
$errors
块引用
答案 1 :(得分:0)
这样做:
def removeDuplicates(oldList):
newList = []
for i in oldlist:
if i not in newlist:
newlist.append(i)
return newList
并像这样使用它:
sentence = input("Please enter a sentence: ")
words = removeDuplicates(sentence.split())
答案 2 :(得分:0)
快速单线。按空格和集合拆分可找到唯一值:
words = list(set(sentence.split(' ')))
print words
答案 3 :(得分:0)
不会words
产生您想要的结果吗?
In [9]: sentence = input("Please Enter as sentence: ")
Please Enter as sentence: 'I like Python'
In [10]: words = sentence.split()
In [11]: words
Out[11]: ['I', 'like', 'Python']
答案 4 :(得分:-1)
您可以使用设置命令删除重复项
sentence = input("Please enter a sentence:")
set(sentence.split())