I am trying to assign an number identifier for words in a string.
I have currently done the following:
mystr = 'who are you you are who'
str_values = [i for i in mystr.split()]
list_values = [str(i) for i, w in enumerate(mystr.split())]
['0', '1', '2', '3', '4', '5']
['who', 'are', 'you', 'you', 'are', 'who']
['0', '1', '2', '2', '1', '0']
You could do this with help of another list -
n =  output = [n.index(i) for i in mystr.split() if i in n or not n.append(i)]
n is empty list. Now list comprehension iterate over all the element of
mystr.split(). It adds the index of the element in list
n if condition met.
Now for the condition. There are two parts with an
or. First it checks if the element is present in
n. If yes, then get the index of the element. If no, it goes to the second part, which just appends the element to the list
None. That is why I added a not before it. So, that condition will be satisfied and it will give the newly inserted elements index.
Basically the first part of if condition restricts duplicate element addition in
n and the second part does the addition.