A Program That Identifies Individual Words In A Sentence, Stores These In A List And Replaces Each Word With The Position Of That Word In The List
Solution 1:
Here's a linear algorithm:
position = {} # word -> position
words = sentence.split()
for word in words:
if word not in position: # new word
position[word] = len(position) + 1# store its positionprint(*map(position.__getitem__, words), sep=",")
# -> 1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5
The print()
call uses Python 3 *
syntax to unpack the result returned by map()
that returns positions for the corresponding words here. See What does ** (double star) and * (star) do for parameters?
Solution 2:
To get a list of word positions in sentence
and recreate the original sentence from this list:
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
positions = [s.index(x)+1 for x in s]
recreated = [s[i-1] for i in positions]
# the reconstructed sentenceprint(" ".join(recreated))
# the list of index numbers/word positionsprint(positions)
# the positions as a space separated string of numbersprint(" ".join(positions)
Lists are zero-indexed so the first element is index 0, not 1. You could, of course, add 1 to all indices in the list comprehension if you wanted it to start at 1.
To get exactly the same output as your script produces:
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
positions = [s.index(x)+1 for x in s]
print(sentence)
print(positions)
Output:
ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5]
Solution 3:
sentence = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'
words = sentence.split()
# Counting backwards through the words means the last seen one will have # the lowest index without needing 'if' tests or incrementing counters.
positions = {word:indexforindex, word in reversed(list(enumerate(words, 1)))}
print(' '.join(str(positions.get(word)) for word in words))
Try it on repl.it here: https://repl.it/CHvy/0
Solution 4:
Not terribly efficient, but two lines.
words = sentence.split()
positions = [words.index(word) + 1 for word in words]
Note that list.index(entry)
will return the index of the first occurrence of entry
. If you're okay with 0-based indices, then the following is pretty concise:
positions = list(map(words.index, words))
Post a Comment for "A Program That Identifies Individual Words In A Sentence, Stores These In A List And Replaces Each Word With The Position Of That Word In The List"