简体   繁体   中英

Creating a function that takes a list of words, and return a set of words in the sentence

  1. need to create a function that takes a string

eg "hello it is a nice day today and it is hot"

  1. the function needs to use the LIST of words in the string, and create a SET consisting of the words in the string.

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

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.

Try adding the link when you create the set

dickens_words = set(words)

A = list(input("statement:").strip().split(' '))

print (' '.join(list(dict.fromkeys(A))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM