【发布时间】:2017-03-16 08:21:30
【问题描述】:
我有一些代码,我打算打印出用户输入的字符串中的所有唯一单词:
str1 = input("Please enter a sentence: ")
print("The words in that sentence are: ", str1.split())
unique = set(str1)
print("Here are the unique words in that sentence: ",unique)
我可以让它打印出唯一的字母,但不能打印出唯一的单词。
【问题讨论】:
-
只需将分割后的字符串传给set:
set(str1.split()) -
谢谢你,真的有帮助! ;)
标签: python string set python-3.5