python - How to print words (and punctuation) from a list of positions -


(removed code stop class mates copying) right create text file positions of each word, example if wrote

"hello, name mika, hello"

the positions in list [1,2,3,4,5,6,2,1], , list each word/punctuation once, in case

['hello', ',', 'my', 'name', 'is', 'mika']

the thing able words original sentence using positions in list, can't seem do. did try searching other posts seemed come other people wanting positions of words rather wanting put words sentence using positons. thought started doing this:

for in range(len(readlines[1])): 

but have no idea how go around doing this.

edit: has been solved @abhishek, thank you.

indices = [1,2,3,4,5,6,2,1] namelst = ['hello', ',', 'my', 'name', 'is', 'mika']  newstr = " ".join([namelst[x-1] x in indices]) print (newstr) 

output:

>> 'hello , name mika , hello' 

i agree there offsets / spaces, give complete sentence again


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -