Assigning Number To Word In A String In Python
hi i have a compression task in python to develop code where if the input is 'hello its me, hello can you hear me, hello are you listening' Then the output should be 1,2,3,1,4,5,
Solution 1:
An easy way is to use a dict, when you find a new word add a key/value pairing using an incrementing variable, when you have seen the word before just print the value from the dict:
s = 'hello its me, hello can you hear me, hello are you listening'
def cyc(s):
# set i to 1
i = 1
# split into words on whitespace
it = s.split()
# create first key/value pair
seen = {it[0]: i}
# yield 1 for first word
yield i
# for all var the first word
for word in it[1:]:
# if we have seen this word already, use it's value from our dict
if word in seen:
yield seen[word]
# else first time seeing it so increment count
# and create new k/v pairing
else:
i += 1
yield i
seen[word] = i
print(list(cyc(s)))
Output:
[1, 2, 3, 1, 4, 5, 6, 3, 1, 7, 5, 8]
You can also avoid slicing by using iter and calling next to pop the first word, also if you want to make foo == foo! we need to remove any punctuation from the string which cam be done with str.rstrip:
from string import punctuation
def cyc(s):
i = 1
it = iter(s.split())
seen = {next(it).rstrip(punctuation): i}
yield i
for word in it:
word = word.rstrip(punctuation)
if word in seen:
yield seen[word]
else:
i += 1
yield i
seen[word] = i
Solution 2:
How about building a dict with item:index mapping:
>>> s
'hello its me, hello can you hear me, hello are you listening'
>>>
>>> l = s.split()
>>> d = {}
>>> i = 1
>>> for x in l:
if x not in d:
d[x]=i
i += 1
>>> d
{'its': 2, 'listening': 8, 'hear': 6, 'hello': 1, 'are': 7, 'you': 5, 'me,': 3, 'can': 4}
>>> for x in l:
print(x, d[x])
hello 1
its 2
me, 3
hello 1
can 4
you 5
hear 6
me, 3
hello 1
are 7
you 5
listening 8
>>>
If you don't want any punctuations in your split list, then you can do:
>>> import re
>>> l = re.split(r'(?:,|\s)\s*', s)
>>> l
['hello', 'its', 'me', 'hello', 'can', 'you', 'hear', 'me', 'hello', 'are', 'you', 'listening']
Solution 3:
import re
from collections import OrderedDict
text = 'hello its me, hello can you hear me, hello are you listening'
words = re.sub("[^\w]", " ", text).split()
uniq_words = list(OrderedDict.fromkeys(words))
res = [uniq_words.index(w) + 1 for w in words]
print(res) # [1, 2, 3, 1, 4, 5, 6, 3, 1, 7, 5, 8]
Post a Comment for "Assigning Number To Word In A String In Python"