Recreating a sentence from the position of each word in it - python

I am looking to develop a program that identifies individual words in a sentence and replaces each word with the position of each word in the list.
For example, this sentence:
HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON
This contains 11 different words and I'd like my program to recreate the sentence from the positions of these 11 words in a list:
1,2,3,4,5,6,7,8,9,10,5,11,6,7
I'd then like to save this new list in a separate file. So far I have only gotten this:
#splitting my string to individual words
my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"
splitted = my_string.split()

>>> my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"
>>> splitted = my_string.split()
>>> order = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 11, 6, 7
>>> new_str = ' '.join(splitted[el] for el in order)
'I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP IN ME PYTHON PLEASE'
Updated according to your comment:
You are looking for index() method.
my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"
splitted = my_string.split()
test = "I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP IN ME PYTHON PLEASE".split()
print ', '.join(str(splitted.index(el)) for el in test)
>>> 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 11, 6, 7
** we suppose that there are no repeating words

my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"
splitted = my_string.split()
d = {}
l=[]
for i,j in enumerate(splitted):
if j in d:
l.append(d[j])
else:
d[j]=i
l.append(i)
print l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 11, 5, 6]

Try:
>>> from collections import OrderedDict
>>> my_string = "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"
>>> splitted = my_string.split()
>>> key_val = {elem : index + 1 for index, elem in enumerate(list(OrderedDict.fromkeys(splitted)))}
>>> [key_val[elem] for elem in splitted]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 11, 6, 7]
list(OrderedDict.fromkeys(splitted)) create a list having only unique elements from splitted.
key_val is dictionary of these unique elements as key and their index as the value.

Try this:
sentence= "HELLO I NEED SOME HELP IN PYTHON PLEASE CAN YOU HELP ME IN PYTHON"
lst = sentence.split()
lst2= []
for i in lst:
if i not in lst2:
lst2.append(i)
inp = inputSentence.split()
output=[]
for i in inp:
print lst2.index(i)+1,
output.append(lst2.index(i)+1)
The index is evaluated and stored in lst2. You just have to pass your input string to inputSentence , so that you are able to test this code.

benstring = input ('enter a sentence ')
print('the sentence is', benstring)
ben2string = str.split(benstring)
print('the sentence now looks like this',ben2string)
x=len(ben2string)
for i in range(0,x):
if x is

Sentence = input("Enter a sentence!")
s = Sentence.split() #Splits the numbers/words up so you can see them individually.
another = [0]
for count, i in enumerate(s):
if s.count(i) < 2:
another.append(max(another) + 1) #adds +1 each time a word is used, showing the position of each word.
else:
another.append(s.index(i) +1)
another.remove(0)
print(another) #prints the number that word is in.

Related

Python How to make a proper string slicing?

I can't figure out how to properly slice a string.
There is a line: "1, 2, 3, 4, 5, 6". The number of characters is unknown, numbers can be either one-digit or three-digit
I need to get the last value up to the nearest comma, that means I need to get the value (6) from the string
you can try to split and get last value
string = "1, 2, 3, 4, 5, 6"
string.split(',')[-1]
>>> ' 6'
add strip to get rid of the white spaces
string.split(',')[-1].strip(' ')
>>> '6'
Better use str.rsplit, setting maxsplit=1 to avoid unnecessarily splitting more than once:
string = "1, 2, 3, 4, 5, 6"
last = string.rsplit(', ', 1)[-1]
Output: '6'
It seems to me the easiest way would be to use the method split and divide your string based on the comma.
In your example:
string = '1, 2, 3, 4, 5, 6'
last_value = string.split(', ')[-1]
print(last_value)
Out[3]: '6'
Here's a function that should do it for you:
def get_last_number(s):
return s.split(',')[-1].strip()
Trying it on a few test strings:
s1 = "1, 2, 3, 4, 5, 6"
s2 = "123, 4, 785, 12"
s3 = "1, 2, 789654 "
...we get:
print (get_last_number(s1))
# 6
print (get_last_number(s2))
# 12
print (get_last_number(s3))
# 789654
First of all you have to split the string:
string = '1, 2, 3, 4, 5, 6'
splitted_str = string.split(',')
Then, you should get the last element:
last_elem = splitted_str[-1]
Finally, you have to delete the unnecessary white spaces:
last_number_str = last_elem.strip()
Clearly this answer is a string type, if you need the numeric value you can cast the type by using
last_elem_int = int(last_elem_str)
Hope that helps

Python: Convert list of numbers to according letters

I know the answer is going to be obvious once I see it, but I can't find how to convert my output list of numbers back to letters after I've manipulated the list.
I am putting in data here:
import string
print [ord(char) - 96 for char in raw_input('Write Text: ').lower()]
and I want to be able to reverse this so after I manipulate the list, I can return it back to letters.
example: input gasoline / output [7, 1, 19, 15, 12, 9, 14, 5]
manipulate the output with append or other
then be able to return it back to letters.
Everything I search is only to convert letterst to numbers and nothing to convert that list of numbers back to letters.
Thank you!
It can be done by using chr() built-in function :
my_list = [7, 1, 19, 15, 12, 9, 14, 5]
out = ""
for char in my_list:
out += chr( 96 + char )
print(out) # Prints gasoline
If you want the final output as a list of characters use the first one otherwise the last one.
l = [7, 1, 19, 15, 12, 9, 14, 5] # l is your list of integers
listOfChar = list(map(chr,[x+96 for x in l]))
aWord = "".join(list(map(chr,[x+96 for x in l])))#your word here is "gasoline"

Assigning number to word in a string in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
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,6,3,1,7,5,8
Basically each word is assigned a numerical value and if the word is repeated so is the word.
This coding is in python, please help me thank you
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
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']
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]

How to read a two digit numbers in CSV file and storying it in a list?

I am scanning a column in Python, which is full of integers. There are some double digit numbers.
d = []
for Column in ReadDataSourceFile: #ReadDataSourceFile works well. Its file open and delimiter
if Column[1] == 'Something' and Column[0] == 'Somewhere':
countFL += 1
print Column[5]
some = map(int, Column[5])
d.extend(some)
print d
Here Column[5] is 1, 15, 23, 1, 4, 5. But the print displays [1, 1, 5, 2, 3, 1, 4, 5]
Probably some = map(int, Column[5]) split number on the digits
print map(int, '15')
[1, 5]
So print some to check it.
Maybe you need only some = int(Column[5])
EDIT: try
print Column[5]
some = int(Column[5])
d.append(some)
Your first output looks like a string
Maybe this will work:
Column[5].split(',')

python: splitting a long string at distinct places in one run

I am entirely new to programming and just yesterday started learning python for scientific purposes.
Now, I would like to split a single very long string (174 chars) into several smaller as follows:
string = 'AA111-99XYZ '
split = ('AA', 11, 1, -99, 'XYZ')
Right now, the only thing I can think of is to use the slice syntax x-times, but maybe there is a more elegant way? Is there a way to use a list of integers to indicate the positions of where to split, e.g.
split_at = (2, 4, 5, 8, 11)
split = function(split_at, string)
I hope my question is not too silly - I couldn't find a similar example, but maybe I just don't know what I'm looking for?
Thanks,
Jan
Like this:
>>> string = 'AA111-99XYZ '
>>> split_at = [2, 4, 5, 8, 11]
>>> [string[i:j] for i, j in zip([0]+split_at, split_at+[None])]
['AA', '11', '1', '-99', 'XYZ', ' ']
def split_string(string, points):
for left, right in zip(points, points[1:]):
yield string[left:right]
to avoid redundancy, you could take ATOzTOA's nice solution and put it in a lamba-function:
st = 'AA111-99XYZ '
sa = [2, 4, 5, 8, 11]
res = lambda string,split_at:[string[i:j] for i, j in zip([0]+split_at, split_at+[None])]
print(res(st,sa))
Being relatively new to Python myself, I took the approach of a complete beginner here just to help guide someone who isn't yet familiar with the power of Python.
string = 'AA111-99XYZ '
split_at = [2, 4, 5, 8, 11]
for i in range(len(split_at)):
if i == 0:
print string[:split_at[i]]
if i < len(split_at)-1:
print string[split_at[i]:split_at[i+1]]
if i == len(split_at)-1:
print string[split_at[i]:]

Categories