简体   繁体   English

从列表中删除项目

[英]Removing an item from a list

I would like to remove to ignore duplicates in my list. 我想删除以忽略列表中的重复项。 For example, let's say the function checks for words that end with a ''.'' and puts them in a list. 例如,假设该函数检查以``。''结尾的单词并将其放入列表中。 I would like to make sure that duplicate words don't go in the list. 我想确保重复的单词不会出现在列表中。

Here is what I have so far: 这是我到目前为止的内容:

def endwords(sent):
    list = []
    words = sent.split()
    for word in words:
        if "." in word:
            list.append(word)
        # bottom if statment does not work for some reason. thats the one i am trying to fix    
        if (word == list):
            list.remove(word)
    return list

How about you check if the word is already in the list before appending it, like so: 您如何在附加单词之前检查单词是否已经在列表中,如下所示:

def endwords(sent):
     wordList = []
     words = sent.split()
     for word in words:
         if "." in word and word not in wordList:
             wordList.append(word)
     return wordList

You're trying to check if word == list , but that's seeing if the word is equal to the entire list. 您正在尝试检查word == list ,但是正在查看单词是否等于整个列表。 To check if an element is in a container in python, you can use the in keyword. 要检查某个元素是否在python的容器中,可以使用in关键字。 Alternatively, to check if something is not in a container, you can use not in . 另外,要检查容器中是否没有东西,可以在中使用not in

Another option is to use a set: 另一种选择是使用一组:

def endwords(sent):
     wordSet = set()
     words = sent.split()
     for word in words:
         if "." in word:
             wordSet.add(word)
     return wordSet

And to make things a little cleaner, here is a version using set comprehension: 为了使事情更简洁,以下是使用集合理解的版本:

def endwords(sent):
    return {word for word in sent.split() if '.' in word}

If you want to get a list out of this function, you can do so like this: 如果要从此功能中获取列表,可以这样进行:

def endwords(sent):
    return list({word for word in sent.split() if '.' in word})

Since you said in your question you want to check if the word ends with a '.', you probably also want to use the endswith() function like so: 既然您在问题中说过,您想检查单词是否以“。”结尾,那么您可能还想使用endswith()函数,如下所示:

def endwords(sent):
    return list({word for word in sent.split() if word.endswith('.')})

After statement 声明后

list = []

you can't use built-in list class and to understand that you can spend about an hour or so, that's why we avoid names of built-ins for our objects. 您不能使用内置list类,并且不能理解您可以花费大约一个小时的时间,这就是为什么我们避免为对象使用内置名称的原因。

More at this answer . 更多关于这个答案


function checks for words that end with a ''.'' 函数检查以``。''结尾的单词。

Statement 声明

"." in word

checks if word contains dot symbol (eg "." in "sample.text" will work ok while it simply doesn't end with dot), if you need to check that it ends with dot – use str.endswith method. 如果需要检查word包含点符号,则检查word包含点符号(例如"." in "sample.text"而它根本不以点结尾)可以正常工作–使用str.endswith方法。


I would like to make sure that duplicate words don't go in the list. 我想确保重复的单词不会出现在列表中。

just make sure before storing one that it hasn't been stored already. 只要确保在存储之前尚未存储它即可。


Finally we can write 最后我们可以写

def endwords(sent, end='.'):
    unique_words = []
    words = sent.split()
    for word in words:
        if word.endswith(end) and word not in unique_words:
            unique_words.append(word)
    return unique_words

Test 测试

>>>sent = ' '.join(['some.', 'oth.er'] * 10)
>>>unique_words = endwords(sent)
>>>unique_words
['some.']

PS PS

If order doesn't matter – use set , it will take care of duplicates (works only with hashable types, str is hashable): 如果顺序无关紧要,请使用set ,它会处理重复项(仅适用于可哈希类型, str可哈希):

def endwords(sent, end='.'):
    unique_words = set()
    words = sent.split()
    for word in words:
        if word.endswith(end) and word not in unique_words:
            unique_words.add(word)
    return unique_words

or with set comprehension 或具有设定的理解力

def endwords(sent, end='.'):
    words = sent.split()
    return {word for word in words if word.endswith(end)}

You can add a sample judge for the question. 您可以为问题添加样本法官。

def endwords(sent):
    list = []
    words = sent.split()
    for word in words:
        if "." in word:
            if word not in list:
                list.append(word)
        # bottom if statment does not work for some reason. thats the one i am trying to fix   

    return list

Why not use a set? 为什么不使用一套?

def endwords(sent):
    my_list = set()
    words = sent.split()
    for word in words:
        if "." in word:
            my_list.add(word)
    return my_list

The less verbose way to do it would be using list comprehension, that is 较为简单的方法是使用列表理解,即

my_list = [word for word in words if '.' in word]

And to ensure the elements aren't duplicated, just use set . 为了确保元素不重复,只需使用set

my_list = set(my_list)  # No more duplicated values

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

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