简体   繁体   English

在不使用 endswith() 的情况下从列表中删除以后缀结尾的单词

[英]Remove words from a list that end with a suffix without using endswith()

I want to write a python function that takes 2 parameters:我想写一个 python function 有两个参数:

  1. List of words and单词列表和
  2. Ending letters结束字母

I want my function to work in such a way that it modifies the original list of words and removes the words which end with the "ending letters" specified.我希望我的 function 能够修改原始单词列表并删除以指定的“结尾字母”结尾的单词。

For example:例如:

list_words = ["hello", "jello","whatsup","right", "cello", "estello"]
ending = "ello"

my_func(list_words, ending)

This should give the following output:这应该给出以下 output:

list_words = ["whatsup","right"]

It should pop off all the strings that end with the ending letters given in the second argument of the function.它应该弹出所有以 function 的第二个参数中给出的结尾字母结尾的字符串。

I can code this function using the .endswith method but I am not allowed to use it.我可以使用.endswith方法对这个 function 进行编码,但不允许我使用它。 How else can I do this using a loop?我还能如何使用循环来做到这一点?

Try:尝试:

def my_func(list_words, ending):
    return [word for word in list_words if word[len(word)-len(ending):] != ending]

You can easily check for the last4 characters of a string using string[-4:] .您可以使用string[-4:]轻松检查字符串的最后 4 个字符。

So you can use the below code所以你可以使用下面的代码

list_words = ["hello", "jello","whatsup","right", "cello", "estello"]
ending = "ello"

def my_func(wordsArray, endingStr):
    endLen = len(endingStr)
    output = []
    for x in wordsArray:
        if not x[-endLen:] == endingStr:
            output.append(x)
    return output

list_words = my_func(list_words, ending)

You can shorten the function with somelist comprehension like this:您可以使用如下列表理解来缩短 function:

def short_func(wordsArray, endingStr):
    endLen = len(endingStr)
    output = [x for x in wordsArray if x[-endLen:] != endingStr]
    return output

list_words = short_func(list_words, ending)

It is always better to not modify the existing list you can get a list which doesn't have the words with the ending specified like below.最好不要修改现有列表,您可以获得一个没有指定结尾单词的列表,如下所示。 If you want to have it as a function you can have it in a following manner.如果你想把它作为 function 你可以通过以下方式拥有它。 You can assign the formatted list to list_words again.您可以再次将格式化列表分配给 list_words。

def format_list(words, ending):
    new_list = []
    n = len(ending)
    for word in words:
        if len(word) >= n and  n > 0:
            if not word[-n:] == ending:
                new_list.append(word)
        else:
            new_list.append(word)
    return new_list 

list_words = format_list(list_words, ending)
print(list_words)
def filter_words(list_words, ending):
    return [*filter(lambda x: x[-len(ending):] != ending , list_words)]

Not allowed to use endswith ?不允许使用endswith Not a problem:-P没问题:-P

def my_func(list_words, ending):
    list_words[:] = [word for word in list_words
                     if not word[::-1].startswith(ending[::-1])]
    return list_words

Loopholes ftw.漏洞英尺。

(Adapted to your insistence on modifying the given list. You should probably really decide whether to modify or return, though, not do both, which is rather unusual in Python.) (适应您对修改给定列表的坚持。您可能真的应该决定是修改还是返回,但是不要两者都做,这在 Python 中是相当不寻常的。)

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

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