Skip to content Skip to sidebar Skip to footer

Individual Words In A List Then Printing The Positions Of Those Words

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

Solution 1:

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]

Solution 2:

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 inset(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]
>>> 

Solution 3:

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])

Solution 4:

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

Solution 5:

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 inlist(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]

Post a Comment for "Individual Words In A List Then Printing The Positions Of Those Words"