logic - How to sort specific string, in the following situation (PYTHON) -


i trying build acronym shortner (as beginners project)
link:http://pastebin.com/395ig9ec

explaination:
++acronym block++

if user string variable set "international business machines" return ibm

but in the...

++sorting block++ 

if user string variable set "light amplification simulated emission of radiation"

i tried split whole sentence by:

 z=string.split(" ")  l=len(z) 

then use following loop:
'''|soring block|'''<

for x in range(0,l,1):     esc=z[x]     if (z[x]=="by" or z[x]=="the" or z[x]=="of"):             esc=z[x+1]       emp=emp+" "+esc   print emp 

but problem when there 2 consecutive exclusion words python messes up. how solve it?

this takes first letter of each word in sentence, ignores words excluded, , puts letters using join.

#python3 def make_acronym(sentence):     excluded_words = ['by', 'the', 'of']     acronym = ''.join(word[0] word in sentence.split(' ') if word not in excluded_words)     return acronym.upper() 

example:

>>> make_acronym('light amplification simulated emission of radiation') 'laser' 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -