2017-06-05 115 views
0

我想删除以忽略列表中的重复项。例如,假设函数检查以“。”结尾的单词并将它们放入列表中。我想确保重复的单词不在列表中。删除列表中的项目

这里是我迄今为止

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  

请注意,我用Python 3

+0

你应该避免使用内置插件的名称为您的对象(如'list','dict','str'等) –

回答

2

你怎么样检查,如果这个词是已经在列表中追加它,像这样前:

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

您正在尝试检查是否word == list,但是如果该单词与整个列表相同,则显示该单词。要检查一个元素是否在python容器中,可以使用in关键字。或者,要检查某件物品是否不在容器中,可以使用not in

另一种选择是使用一组:

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

使事情干净了一点,这里是使用设置理解一个版本:

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

如果你想获得一个名单出来你可以这样做:

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

既然你说你的问题,你想检查是否吨他的词以结束后,你可能也想使用的endsWith()函数像这样“”:

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

您可以添加一个样本法官的提问。

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 
0

为什么不使用set?

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

的更简洁的方式,将使用列表理解的事,那就是

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

,并保证元素不重复,只是使用set

my_list = set(my_list) # No more duplicated values 
2

语句后

list = [] 

你不能使用内置list class和明白,你可以花大约一个小时左右,这就是为什么我们要避免的内置插件的名称,我们的对象。

更多在this answer


函数检查与一个 '' 结尾的单词。“”

声明如果

"." in word 

检查word包含点符号(如"." in "sample.text"将好的工作,而它根本不点结尾),如果你需要检查它与点结束 - 使用str.endswith方法。


我想,以确保重复的话不要在列表中去。

只是确保在存储尚未存储的文件之前。


最后,我们可以写

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 

测试

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

PS

如果顺序并不重要 - 使用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 

或一套理解

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