Skip to content

Instantly share code, notes, and snippets.

@anddam
Created October 2, 2016 14:01
Show Gist options
  • Save anddam/8588e6a19a6f13056cc45b517b378195 to your computer and use it in GitHub Desktop.
Save anddam/8588e6a19a6f13056cc45b517b378195 to your computer and use it in GitHub Desktop.
Find unique words in a sentence and mark their positions
import string
sentence = input("Please enter a sentence \n")
translator = str.maketrans({key: None for key in string.punctuation})
words = sentence.translate(translator).lower().split()
def indices(source_list, item):
return [index for index, value in enumerate(source_list) if value == item]
indexed_words = {word: indices(words, word) for word in set(words)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment