Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Created March 4, 2020 05:41
Show Gist options
  • Save Eligijus112/4c38cdaf036e2588912d88dde514dd91 to your computer and use it in GitHub Desktop.
Save Eligijus112/4c38cdaf036e2588912d88dde514dd91 to your computer and use it in GitHub Desktop.
Creates a unique word dictionary using from a list of strings
def create_unique_word_dict(text:list) -> dict:
"""
A method that creates a dictionary where the keys are unique words
and key values are indices
"""
# Getting all the unique words from our text and sorting them alphabetically
words = list(set(text))
words.sort()
# Creating the dictionary for the unique words
unique_word_dict = {}
for i, word in enumerate(words):
unique_word_dict.update({
word: i
})
return unique_word_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment