python - How can I get a loop to ignore non-letter elements in a list? -
i have string contains letters , punctuation. i'm trying replace letters in string other letters. function have developed works strings contain letters. if numbers included produces logic error , if punctuation included produces run-time error. there anyway can function ignore punctuation , leave while operating on letters?
#create string variable, abjumbler generates alphabet shifted x units right #abshifter converts string using 1 type textobject = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj." smalltext = 'abcde' alphabet = list(['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']) def abjumbler(alphabet, x): freshset = [] i=0 j=0 while i<(len(alphabet)-x): freshset.extend(alphabet[i+x]) i+=1 while j<x: freshset.extend(alphabet[j]) #extend [0] j+=1 #change j = 1, extends [1], [2], , terminates when reaches x alphabet = freshset return alphabet newalphabet = abjumbler(alphabet, 2) def abshifter(text, shiftedalphabet): freshset = [] letters in text: position = text.index(letters) freshset.extend(shiftedalphabet[position]) final = ''.join(freshset) return final print abshifter(smalltext, newalphabet)
for one, there faster/simpler ways of doing shifting want.
but answer question, add:
if not letter.isalpha(): continue
str.isalpha()
returns true
if string composed of alphabetic letters.
Comments
Post a Comment