简体   繁体   English

如何应用两个'如果; 在 python 中定义

[英]How to apply two 'if; in a def in python

input=('befelled','recalled','expelled','swelled','tested','marked','scott','brutt')

I want an output which looks like,我想要一个看起来像的输出,

output=('befel','recal','expel','swel','test','mark','scott','brutt')

It's like if the word ends with 'ed', remove the 'ed' else return to the similar word, the second condition would be if the word ends with 'll' after applying the first condition then remove the 'l' and return the output就像如果单词以 'ed' 结尾,则删除 'ed' 否则返回相似的单词,第二个条件是,如果单词在应用第一个条件后以 'll' 结尾,然后删除 'l' 并返回输出

I want to apply two ifs我想申请两个ifs

First if will check for all the words which end with 'ed' and then this if will remove the last two alphabets from the words which satisfy the first if.首先 if 将检查所有以 'ed' 结尾的单词,然后这个 if 将从满足第一个 if 的单词中删除最后两个字母。

Then I want to aplly the second if which will look for all the word s which end with 'll'然后我想应用第二个 if 它将查找所有以 'll' 结尾的单词 s

words=('befelled','recalled','expelled','swelled','tested','marked','scott','brutt') . 
def firstif(words):  
    for w in words:  
        if w.endswith('ed'):  
             return (w[:-2]) . 
        else:  
            return(w) . 
firstif(w) . 
words2=tuple(firstif(w)) . 
def secondif(words2):  
    for w2 in words2:  
        if w2.endswith('ll'):  
            return (w2[:-1]) . 
        else:  
            return(w2) . 
secondif(w2)

This code is running but giving me weird output这段代码正在运行,但给了我奇怪的输出

Using nested loops to check would be a better idea.使用嵌套循环来检查会是一个更好的主意。 Just try:你试一试:

words=('befelled','recalled','expelled','swelled','tested','marked','scott','brutt') . 
def fix_word(words):
    temp = []
    for w in words:
        if w.endswith('ed'):
            if w.endswith('lled'):
                temp.append(w[:-3])
            else:
                temp.append(w[:-2])
        else:
            temp.append(w)
    return tuple(temp)

I can also use elif我也可以使用elif

words=('befelled','recalled','expelled','swelled','tested','marked','scott','brutt')
a=[]
for w in words:
    if w.endswith("lled"):
        a.append(w[:-3])    
    elif w.endswith("ed"):
        a.append(w[:-2])
    else:
        a.append(w)

result结果

>>>tuple(a)
('befel', 'recal', 'expel', 'swel', 'test', 'mark', 'scott', 'brutt')

The alternative way by using map()使用map()的替代方法

Note: I don't recommend you to use input as a variable name since there's a function called input() as well.注意:我不建议您使用input作为变量名,因为还有一个名为input()的函数。

def check(word):
    temp_word = word[:-2] if word.endswith('ed') else word
    return temp_word[:-1] if temp_word.endswith('ll') else temp_word

user_input=('befelled','recalled','expelled','swelled','tested','marked','scott','brutt')

res = tuple(map(check, user_input))

Result:结果:

res
('befel', 'recal', 'expel', 'swel', 'test', 'mark', 'scott', 'brutt')

You could solve it in pythonic way (one line)你可以用pythonic的方式解决它(一行)

words = ['befelled','recalled','expelled','swelled','tested','marked','scott','brutt']
clean_words = [(e[:-3] if e.endswith('lled') else e[:-2] if e.endswith('ed') else e ) for e in words ]
You can also use slicing.    
inputs=('befelled','recalled','expelled','swelled','tested','marked','scott','brutt')

    def clean_word(words):
      result = []
      for word in words:
        if word[-4:] == 'lled':
          result.append(word[:-3])
        elif word[-2:] == 'ed':
          result.append(word[:-2])
        else:
          result.append(word)
      return result

    print(clean_word(inputs))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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