__author__ = "Brian Woodward" #Python Code excersise # Parse a sentence and replace each word with the following: # first letter, number of distinct characters between first and last character, and last letter, # For example, Smooth would become S3h. # Words are separated by spaces or non-alphabetic characters print("") print("Thank you for taking the time to review my code, I hope like it!") input = "Please enter your sentence here." print("") print("") print(input) print("") print("") #Find the fist letter in each word print("Find the first letter in each word") first_letters = [ i[0].upper() for i in input.split() ]; output = "".join(first_letters) print(first_letters) print("") #Find the number of characters between the first and last character in the word print("Find the number of characters between the first and last word") words = input.split() letter_count_per_word = [(len(w)-2) for w in words] print(letter_count_per_word) print("") #Find the last character in each word print("Find the last character in each word") last_character = [i[-1].lower() for i in input.split()]; output = "".join(last_character) print(last_character) print("") print("") # Zip lists together to create end result print("Add the First Letter, # of characters between first and last character in the word, and the last character of the word to create new desired word") # Having an issue after combining the variables, the end result does not sequence the words in the same pattern as the sentence. A2 = (first_letters[:]) B2 = (letter_count_per_word[:]) C2 = (last_character[:]) D = zip(A2, B2, C2) E = sorted(D, key=lambda x: x[1]) A2, B2, C2 = zip(*E) print(E) # Question: How to merge the elements within an array down to one object in the array. # Question: How to display the zipped array in the same format as the sentence.
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question