将单词添加到包含字符串中某些字母的列表中

时间:2014-05-14 11:54:51

标签: python python-2.7

我有一个字符串,我想找到包含' th'在其中,并将它们添加到列表中。但我不想要一个含有'。'

的词

最终名单中不能包含任何重复的单词。

thestring = "The character that can fire the. bullet that sheriff dodged"
a = "th"
b = "T"

def makelists(thestring, a, b)
    """
    >>> makelists(thestring, 'th', 'T')
    ['that', 'the.']
    """

到目前为止,我只有这个,并且正在打印出重复的单词。

def makelists(thestring, a, b)
    words = thestring.split()
    thelist = [] 
    for word in words:
        if a in word:
            thelist.append(word)           
    for char in thelist:
        if b in char:
            thelist.remove(char)
    print thelist

我得到的输出是[''' the。',' that']。

我的代码可以进行哪些编辑,输出将是['',' the。']

3 个答案:

答案 0 :(得分:2)

虽然您的代码很长并且您必须对其进行优化,但您可以在添加列表之前进行检查: -

def makelists(thestring, a, b)
    words = thestring.split()
    thelist = [] 
    for word in words:
        if a in word and word not in thelist:
            thelist.append(word)           
    for char in thelist:
        if b in char:
            thelist.remove(char)
    print thelist

或者,另一个解决方案是: -

thelist = list(set(thelist))

答案 1 :(得分:0)

使用一个集合,您甚至可以使用更好的if语句缩短代码:

def makelists(thestring, a, b):
    words = thestring.split()
    thelist = set([]) 
    for word in words:
        if a in word and b not in word:
            thelist.add(word)          
    print thelist

答案 2 :(得分:0)

尝试使用re模块和列表推导,如下所示:

import re
thestring = "The character that can fire the. bullet that sheriff dodged"    
a = "th"
b = "T"

print list(set([word  for word in re.split(" +", thestring) if a in word and b not in word ]))