简体   繁体   中英

How to add characters at the end of each element in a list

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.

  1. A list of words is assigned to the variable "words."
  2. A new empty list is created using [] and assigned to the variable "past_tense." Past-tense words will be converted from present-tense words in the list "words" and added to this list.
  3. The iterator "word" iterates over the iterable "words," beginning with the first string in the list "words." The "for loop" ensures that the subsequent indented commands will be repeated on each word in the sequence (9 words).
  4. Starting with the first word in "words," the "if" statement evaluates the last character in each word. word[-1] denotes the last character in the word (indexing operator). If the condition is evaluated to be true (if the final letter in the word is an "e"), the word is added (using the "append" method) to the list "past_tense" with a "d" concatenated at the end. If the condition is evaluated to be false (the final letter is not an "e"), the "else" condition will apply and the word will be added to the past_tense list with "ed" concatenated at the end.
  5. When the for loop finishes after the final word is evaluated, python will exit the loop. Then, using the print statement, the output of the list "past_tense" will be: ['adopted', 'baked', 'beamed', 'confided', 'grilled', 'planted', 'timed', 'waved', 'wished']
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM