[英]Creating a function that takes a list of words, and return a set of words in the sentence
eg "hello it is a nice day today and it is hot"例如“你好,今天天气很好,很热”
the output should be: ("hello" "it" "is" "a" "nice" "day" "today" "and" "hot") NOTE: the set only has unique words from the sentence, no repeating words output 应该是:(“hello”“it”“is”“a”“nice”“day”“today”“and”“hot”) 注意:该集合只有句子中的唯一词,没有重复词
I tried it myself but it says its wrong:我自己试过了,但它说错了:
opening_line="It was the best of times, it was the worst of times"
def get_vocabulary(word_list):
words = word_list.split()
dickens_words = set()
dickens_words.add(words)
return words
print(get_vocabulary(opening_line))
You're adding the entire list as a single element to the set.您将整个列表作为单个元素添加到集合中。 Instead, you could construct a set from list, which would add all the words individually to the set:
相反,您可以从列表中构造一个集合,它将所有单词单独添加到集合中:
def get_vocabulary(word_list):
return set(word_list.split())
The problem is add() accept only one element.问题是 add() 只接受一个元素。
Try adding the link when you create the set创建集合时尝试添加链接
dickens_words = set(words) dickens_words = 设置(单词)
A = list(input("statement:").strip().split(' ')) A = list(input("statement:").strip().split(' '))
print (' '.join(list(dict.fromkeys(A)))) print (' '.join(list(dict.fromkeys(A))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.