0

Here is my code,

 userinput = input("Enter a sentence: ")
 wordlist = userinput.split()
 uniquelist = []
 for word in wordlist:
     if word not in uniquelist:
         uniquelist.append(word)
 print ("Here are the words in their first appearing index form: ")
 my_indexes = ' '.join(str(uniquelist.index(word)+1) for word in wordlist)
 print (uniquelist)
 print (my_indexes)

It asks the user for an input, a sentence without punctuation, and the program return the positions of each word in that sentence. If any words occur more than once, it outputs the index position of the first time it occurred.

For eg: if the input was - "I like to code because to code is fun" . The output would be -

1 2 3 4 5 3 4 6 7

How would i convert the output, i guess its a string? I am not definite for sure hence the vague title - to a list which has the format

[1,2,3,4,5,3,4,6,7]

?

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
tpullemku
  • 37
  • 1
  • 7
  • this question has already been answered [here](http://stackoverflow.com/questions/41761126/indexes-of-a-list-python/41761206#41761206) – hansaplast Jan 21 '17 at 16:47
  • is that coming from a homework assessment? How comes this same peculiar question is posted twice in the same week? – hansaplast Jan 21 '17 at 16:50
  • if someone could upvote my - IMO correct - answer to this question in [over here](http://stackoverflow.com/questions/41761126/indexes-of-a-list-python/41761206#41761206) then I could close this question as duplicate – hansaplast Jan 21 '17 at 16:51
  • @hansaplast Thank you, not a homework but part of an assessment for 20 weeks, i thought i might as well do it now and then save time to revise instead of finishing coursework in exam times later on :-) – tpullemku Jan 21 '17 at 16:52
  • @hansaplast ill delete it – tpullemku Jan 21 '17 at 16:52
  • @hansaplast Your answer in the linked question isn't the most efficient solution. You used the wrong data structure – the_constant Jan 21 '17 at 16:52
  • @Vincenzzzochi You seem right, i cant link it to my code – tpullemku Jan 21 '17 at 16:54
  • @tpullemku are you keeping this post up? I'll answer here if so – the_constant Jan 21 '17 at 16:55
  • @Vincenzzzochi Yes i am – tpullemku Jan 21 '17 at 16:56
  • Also, is `is fun` positions 6 and 7 or 8 and 9? Because if it's the former, you're not counting duplicates for position and I need to know for the solution. I know you said 6 and 7, I just want to be sure that wasn't in error. – the_constant Jan 21 '17 at 17:09
  • @Vincenzzzochi 6,7 – tpullemku Jan 21 '17 at 17:11
  • @Vincenzzzochi It becomes positions 6,7 because of the repetitive words when we as humans read it, it is 8,9 - it is in this format – tpullemku Jan 21 '17 at 17:13
  • Possible duplicate of [Replacing python list elements with key](http://stackoverflow.com/questions/37603164/replacing-python-list-elements-with-key) – user2390182 Jan 21 '17 at 17:15

2 Answers2

1

You should not use ìndex in the first place, as it is O(N), which will hurt performance for large wordlists. A better way includes using enumerate to create a dict from words to their unique indexes and then using that mapping to build the list of unique indexes:

> wordlist = userinput.split()
> id_s = {c: i for i, c in enumerate(set(wordlist), start=1)}
> id_s
{'code': 0, 'like': 1, 'I': 2, 'is': 3, 'to': 4, 'because': 5, 'fun': 6}
> [id_s[c] for c in list]  
[1, 6, 7, 3, 2, 7, 3, 5, 4]
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

In your current code you are using generator expression and then you are joining the values in order to get the current output string at:

my_indexes = ' '.join(str(uniquelist.index(word)+1) for word in wordlist)  

Instead, if you also want the intermediate list, then you may break this line using list comprehension as:

# As you mentioned in output, you need list of integer indexes
index_list = [uniquelist.index(word)+1 for word in wordlist]  # list of indexes
str_index_list = list(map(str, index_list))  # convert list of `int` index as `str` index

# If you want list of `str` indexes. You may replace the above two line
# via using generator expression in your code as list comprehension as:
#     index_list = [str(uniquelist.index(word)+1) for word in wordlist]

my_indexes = ' '.join(str_index_list)  # value of `str` in your current code
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126