My_List = ["adopt", "bake", "beam"]
Problem : For each word in My_List, add 'd' to the end of the word if the word ends in “e” to make it past tense. Otherwise, add 'ed' to make it past tense. Save these past tense words to a list called Past_Tense.
You can use a list comprehension and f-strings
to add d
to those strings that end with e
, which can be checked using the endswith
str
method:
[f'{i}d' if i.endswith('e') else f'{i}ed' for i in My_List]
# ['adopted', 'baked', 'beamed']
For python versions 3.6 <
use:
['{}d'.format(i) if i.endswith('e') else '{}ed'.format(i) for i in My_List]
So here are some ways to get started:
for word in My_List:
# do something with your word
word[-1]
gives you the last character of the word
.
You can join strings together like this: "{} something else here".format(word)
Put all of them together in a logical order and you can solve the problem on your own! Which is more fun!
this is a way to do that:
My_List = ["adopt", "bake", "beam"]
res = [word + 'd' if word[-1] == 'e' else word + 'ed' for word in My_List]
# ['adopted', 'baked', 'beamed']
Past_Tense = [ w + "e"*(w[-1]!="e") + "d" for w in My_List ]
或者
Past_Tense = [ w + 'ed'[w[-1]=="e":] for w in My_List ]
A little code golfing for those who love. Don't do this in production though. You will be hated for eternity. I take no responsibility :)
>>> l
['adopt', 'bake', 'beam']
>>> x = [x+['ed', 'd'][x.endswith('e')] for x in l]
>>> x
['adopted', 'baked', 'beamed']
Try this :
Past_Tense = [k+'d' if k.endswith('e') else k+'ed' for k in My_List]
OUTPUT :
['adopted', 'baked', 'beamed']
past_tense=[]
for i in words:
if i[-1]=='e':
words.append('d')
else:
words.append('ed')
past_tense=words
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense=[]
for word in words:
if word[-1]=='e':
past_tense=[word+'d']
print(past_tense)
else:
past_tense=[word+'ed']
print(past_tense)
I guess this is how a beginner like me would go about it:
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
length = len(words)
past_tense = []
strLen = 0
for i in range(0, length):
str = words[i]
strLen = len (str)
if str[-1] == 'e':
words[i] = words[i] + 'd'
else:
words[i] = words[i] + 'ed'
past_tense = past_tense + [words[i]]
print(past_tense)
Problem: For each word in words, add 'd' to the end of the word if the word ends in “e” to make it past tense. Otherwise, add 'ed' to make it past tense. Save these past tense words to a list called past_tense.
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for word in words:
if word[-1] == "e":
past_tense.append(word + "d")
else:
past_tense.append(word + "ed")
print(past_tense)
Just one more way to complete the collection. Via the map()
function:
My_List = ["adopt", "bake", "beam"]
def ed(word): return word+"d" if word[-1]=="e" else word+"ed"
Past_Tense = list(map(ed, My_List)) # ['adopted', 'baked', 'beamed']
Here is the correct way to answer this...
wrds = ["end", 'work', "play", "start", "walk", "look", "open", "rain", "learn", "clean"]
past_wrds = []
for i in wrds:
past_wrds.append(i+'ed')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.