Why Does My Code Print Off 18 Instead Of 10, And Could You Provide Any Solutions?
Solution 1:
In your sentence, the list becomes:
['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU', 'ASK', 'WHAT', 'YOU', 'CAN', 'DO', 'FOR', 'YOUR', 'COUNTRY', 'POTATO']
POTATO
is the 18th element. You should create a new list with no duplicates:
singles = []
for word in words:
if word not in singles:
singles.append(word)
You can then use singles.index(word)
instead of words.index(word)
. Even better:
used = {}
index = 0
for word in words:
if word in used:
print(used[word])
else:
index += 1
print(index)
used[word] = index
Yes, it's longer, but it is more efficient.
Solution 2:
The problem
You're expecting your code to give you the index of the word without counting repeated words, but you're getting simply the word index in your original string.
The solution
First you need to get the unique words in the original string, so that you get the right word index as per your needs. You can try a demo here. With the Potato
extra word, it returns the index 10 instead of 18, since it looks for it in the unique list, instead of the original one.
string = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY POTATO'
words = string.split()
unique_words = []
#Remove the duplicates while preserving order
for word in words:
if word not in unique_words:
unique_words.append(word)
#Generate the indexes for the words
indexes = [unique_words.index(word)+1 for word in words]
print(indexes)
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5, 10]
Solution 3:
So here's your list of words, with the potato example:
>>> y = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY POTATO"
>>> words = y.split()
>>> print([words.index(s)+1 for s in words])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5, 18]
The reason it prints '18' is that the word "potato" hasn't appeared before, and there are 18 items in the list of words:
>>> for word_number, word in enumerate(words):
... print(word_number+1, word)
...
1 ASK
2 NOT
3 WHAT
4 YOUR
5 COUNTRY
6 CAN
7 DO
8 FOR
9 YOU
10 ASK
11 WHAT
12 YOU
13 CAN
14 DO
15 FOR
16 YOUR
17 COUNTRY
18 POTATO
index()
returns the first time it finds that item in the list. Potato wasn't in the sentence previously, so the last index is returned, which is 17+1.
Post a Comment for "Why Does My Code Print Off 18 Instead Of 10, And Could You Provide Any Solutions?"