input=('befelled','recalled','expelled','swelled','tested','marked','scott','brutt')
我想要一个看起来像
output=('befel','recal','expel','swel','test','mark','scott','brutt')
就像如果单词以'ed'结尾,去掉'ed'否则返回到类似的单词,第二个条件是如果单词在应用第一个条件后以'll'结尾,然后去掉'l'并返回输出
我想申请两个条件
首先if将检查以“ed”结尾的所有单词,然后这个if将从满足第一个if的单词中删除最后两个字母。你知道吗
然后我想应用第二个if,它将查找所有以'll'结尾的单词
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)
这段代码正在运行,但给我奇怪的输出
我也可以使用
elif
结果
使用嵌套循环来检查将是一个更好的主意。试试看:
另一种方法是使用map()
注意:我不建议您使用input作为变量名,因为还有一个名为input()的函数。你知道吗
结果:
相关问题 更多 >
编程相关推荐