编写一个名为containsLetter的函数,该函数标识列表中包含指定字母的所有字符串,并返回这些字符串的列表

时间:2014-09-24 03:14:52

标签: python for-loop parameters function

searchLetter = str(input("Enter a Letter: ")
hulkLine= ["you", "wouldn't", "like", "me", "when", "i'm", "angry"]                   
def containsLetter(searchLetter, hulkLine):
    for x in range(hulkLine -1):
            if hulkLine[x] == 't':

我发布了以下函数的正确输出。

函数containsLetter()有两个参数:

  1. 长度为1的字符串aLetter
  2. 字符串列表strList
  3. 例如,以下是正确的输出:

    >>> hulkLine = ["you", "wouldn't", "like", "me", "when", "i'm", "angry"] 
    >>> searchLetter = 'i' 
    >>> print(containsLetter(searchLetter, hulkLine)) 
    >>> ["like", "i'm"]
    

1 个答案:

答案 0 :(得分:2)

这可以吗?

>>> def containsLetter(searchLetter, hulkLine):
...     return [x for x in hulkLine if searchLetter in x]
...
>>> containsLetter('i', hulkLine)
['like', "i'm"]
>>>

>>> filter(lambda x: searchLetter in x, hulkLine)
['like', "i'm"]
>>>