0

This is my current progress. I'm somewhat new to Python and I'm kind of lost. I don't really know exactly how to solve the problem and I apologies if the title is somewhat misleading. I'll try and explain my problem as best I can.


The output needs to be this in list form:

"WHAT", "IS", "MINE", "IS", "YOURS", "AND", "WHAT", "IS", "YOURS", "IS", "MINE"

"1", "2", "3", "2", "4", "5", "1", "2", "4", "2", "3"

"1" is "WHAT", "2" is "IS"... and so on. If the word appears more than once, it will remain the same number.


Code

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE";

sentence = sentence.lower();

sentence = sentence.split();

uniqueWord = [];

store = [];

for i in sentence:
    if i not in uniqueWord:
        uniqueWord.append(i);

lengthOfUniqueWord = len(uniqueWord);

print(sentence);

print(uniqueWord);

for i in range(lengthOfUniqueWord):
    i = str(i+1);
    store.append(i);

print(store);

for positions in enumerate(uniqueWord, 1):
     print(positions);

Output

['what', 'is', 'mine', 'is', 'yours', 'and', 'what', 'is', 'yours', 'is', 'mine']
['what', 'is', 'mine', 'yours', 'and']
['1', '2', '3', '4', '5']
(1, 'what')
(2, 'is')
(3, 'mine')
(4, 'yours')
(5, 'and')
1
  • Welcome to the site! See the tour for more about how to get help most effectively. Would you please edit your question to include the output you are currently getting in addition to the output you want (already included)?
    – cxw
    Jun 19, 2016 at 18:02

4 Answers 4

2

This should work:

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE";    
sentence = sentence.lower();
sentence = sentence.split();

uniqueWord = [];

for i in sentence:
    if i not in uniqueWord:
        uniqueWord.append(i);

for word in sentence:
    print uniqueWord.index(word) + 1

Here it is the link to the documentation of index

0
0

While others also typed there answers ;-) I came up with - hope it contains further hints on Python coding also:

#! /usr/bin/env python
from __future__ import print_function


def word_indexer(text):
    """Split the text in words maintaining order and return two
    aligned lists: 1) the words all in sequence, and 2) the matching
    unique 1-based case insensitive index (insert based rank)."""

    words_in_order = text.split()
    word_index = []
    unique_word_rank = {}
    rank = 1
    for word in words_in_order:
        normalized_word = word.lower()
        if normalized_word not in unique_word_rank:
            unique_word_rank[normalized_word] = rank
            rank += 1
        word_index.append(unique_word_rank[normalized_word])

    return words_in_order, word_index


def main():
    """Do the word indexing."""
    sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE"
    words_in_order, word_index = word_indexer(sentence)
    # print as in question in two lines:
    print(words_in_order)
    print(word_index)
    # to display like a table:
    for ndx, word in zip(word_index, words_in_order):
        print(ndx, word)

if __name__ == '__main__':
    main()

This gives on my system (with Python 2.7.11 but also runs with Python 3.5.1):

['WHAT', 'IS', 'MINE', 'IS', 'YOURS', 'AND', 'WHAT', 'IS', 'YOURS', 'IS', 'MINE']
[1, 2, 3, 2, 4, 5, 1, 2, 4, 2, 3]
1 WHAT
2 IS
3 MINE
2 IS
4 YOURS
5 AND
1 WHAT
2 IS
4 YOURS
2 IS
3 MINE

Hope this helps - and happy hacking!

2
  • 1
    Thanks for licensing your contributions under a broader license! I have also done so and am always encouraged when I see others making site users' lives easier! :)
    – cxw
    Jun 19, 2016 at 18:17
  • 1
    Great meta feedback :-) Not that I consider any of my co(de|n)tributions important myself, but allowing others to copy and paste answers without fear, is to me an important if not fundamental aspect of a Q&A coding site. After all we encourage the people asking to provide copy and paste ready to run code ...
    – Dilettant
    Jun 19, 2016 at 18:20
0

Here is another method of achieving the same goal:

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE".lower().split()
uniqueWord = list(set(sentence))

print(sentence);
print(uniqueWord);

store = [uniqueWord.index(x) for x in sentence]

print(store);
0

Set up a count iterator, that only increments if a new word is inserted into dict d. Build the final list from the matching number of each word:

from itertools import count

sentence = "WHAT IS MINE IS YOURS AND WHAT IS YOURS IS MINE"  
s =  sentence.split()
c = count(1)
d = {}

for word in s:
    if word.lower() not in d:
         d[word.lower()] = next(c)

result = [str(d[word.lower()]) for word in s]
# ['1', '2', '3', '2', '4', '5', '1', '2', '4', '2', '3']