Reputation: 23
I need help with a program that identifies individual words in a sentence, stores these in a list and replaces each word in the original sentence with the position of that word in the list. Here is what I have so far.
for example:
'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR' COUNTRY
would recreated as 1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5
from collections import OrderedDict
sentence = input("Please input a sentence without punctuation").upper()
punctuation = ("`1234567890-=¬!£$%^&*()_+\|[];'#,./{}:@~<>?")
FilteredSentence = ("")
for char in sentence:
if char not in punctuation:
FilteredSentence = FilteredSentence+char
FilteredSentence = FilteredSentence.split(" ")
refined = list(OrderedDict.fromkeys(FilteredSentence))
I have managed to identify the individual words in the list however I work out how to replace the words in the original list with the positions of the individual words.
Upvotes: 2
Views: 4662
Reputation: 10951
You can also create a dictionary mapping the words with their initial position, then use it to substitute the words with their respective positions.
>>> s = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'
>>>
>>>
>>> l = s.split()
>>> l
['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU', 'ASK', 'WHAT', 'YOU', 'CAN', 'DO', 'FOR', 'YOUR', 'COUNTRY']
>>>
>>> d = dict((s, l.index(s)+1) for s in set(l))
>>> d
{'DO': 7, 'COUNTRY': 5, 'CAN': 6, 'WHAT': 3, 'ASK': 1, 'YOUR': 4, 'NOT': 2, 'FOR': 8, 'YOU': 9}
>>> list(map(lambda s: d[s], l))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5]
>>>
Upvotes: 4
Reputation: 42607
Just a refinement of an existing answer - you can use a dictionary comprehension to create the lookup from word to index:
>>> lookup = {w:(i+1) for i, w in list(enumerate(refined)) }
>>> lookup
{'DO': 7, 'WHAT': 3, 'FOR': 8, 'COUNTRY': 5, 'NOT': 2, 'CAN': 6,
'ASK': 1, 'YOU': 9, 'YOUR': 4}
Then use a list comprehension to generate the output indices:
>>> [lookup[w] for w in FilteredSentence]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5]
Upvotes: 0
Reputation: 121
Instead of the OrderedDict you could just use a set.
refined = list(set(FilteredSentence))
than you can check each word against the list.
index_list = []
for word in FilteredSentence:
index_list.append(refined.index(word) +1)
index_list is the result you asked for
Upvotes: 0
Reputation: 383
The Naive Way of doing it.
mystr = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
a = mystr.split(" ")
k = dict()
cnt = 1
b = []
for m in a:
if m not in k:
k[m] = str(cnt)
cnt = cnt + 1
b.append(k[m])
print ",".join(b)
The Shorter version.
mystr = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
a = mystr.split(" ")
print ",".join([str(a.index(k)+1) for k in a])
Upvotes: 0
Reputation: 191738
Like this? Just do a list-comprehension to get all the indices of all the words.
In [77]: sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
In [78]: words = sentence.split()
In [79]: [words.index(s)+1 for s in words]
Out[79]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5]
Upvotes: 10