-
-
Save cibofdevs/45ce3c620da9714379044dbdce441744 to your computer and use it in GitHub Desktop.
# Write code to create a list of word lengths for the words in original_str, | |
# using the accumulation pattern and assign the answer to a variable num_words_list. | |
# (You should use the len function). | |
original_str = "The quick brown rhino jumped over the extremely lazy fox" | |
original_list = list(original_str.split()) | |
num_words = len(original_list) | |
num_words_list = [] | |
for i in original_list: | |
num_words_list.append((len(i))) | |
print(num_words_list) |
Simplest:
original_str = "The quick brown rhino jumped over the extremely lazy fox"
original_str_list = original_str.split()
num_words_list = []
for word in original_str_list:
num_words_list.append(len(word))
#1. This is original string
#2. Here, we convert words in string to individual elements in list
#2. ['The', 'quick', 'brown', 'rhino', 'jumped', 'over', 'the', 'extremely', 'lazy', 'fox']
#3. Creating empty list to hold length of each word
#4. for loop to iterate over every element (word) in element
#4. for loop working - First it calculates length of every element in list one by one and then appends this length to num_words_list varaible
Even more simplest:
num_words_list = []
original_str = "The quick brown rhino jumped over the extremely lazy fox"
for word in original_str.split():
num_words_list.append(len(word))
a simple one:
original_str = "The quick brown rhino jumped over the extremely lazy fox"
original_list =original_str.split()
num_words_list = []
for i in original_list:
num_words_list=num_words_list+[len(i)]
print(num_words_list)