0
def isUpper(x):
    if ord(x)>64 and ord(x)<91:
        return True
    else:
        return False

def isLower(x):
    if ord(x)>96 and ord(x)<123:
        return True
    else:
        return False

编写完上述函数后,我现在需要编写函数 letterSplit,只使用 isUpper、isLower 和高阶过滤函数。

我想出了:

def letterSplit(strng):
  if strng=='':
    return 0
  else:
    for x in strng:
        for y in strng:    
            filter(isUpper(x),strng)
            filter(isLower(y),strng)
    return (x,y)

出现的错误说 bool obj not callable。请帮帮我~不太了解过滤功能

4

5 回答 5

2

如果我理解您的问题,那么我认为您正在尝试从字符串中过滤掉大写和小写字母。

def letterSplit(strs):
    if not strs: return
    else:
        #filter(isUpper,strs) is equivalent to "".join([x for x in strs if isUpper(x)])

        upper = filter(isUpper,strs)  #filter's first argument is simply function name
        lower = filter(isLower,strs)

        return upper, lower
        # in py3x filter returns an iterator, 
        # so you'll have to use "".join(upper), "".join(lower)

print letterSplit("SomeWeirdLookingStriNG")   

输出:

('SWLSNG', 'omeeirdookingtri')

帮助filter:_

>>> filter?
Type:       builtin_function_or_method
Docstring:
filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true.  If
function is None, return the items that are true.  If sequence is a tuple
or string, return the same type, else return a list.
于 2013-05-08T05:09:12.010 回答
2

问题是那isUpper是你的函数,并且isUpper(x)是一个布尔值(isUpper使用参数 as 评估x)。因此,您实际上想要的似乎是这样的:

def letterSplit(strng):
  return (filter(isUpper,strng),filter(isLower,strng)) if strng else 0

更详细地说,这是:

def letterSplit(strng):
    if not strng:
        return 0
    uppers = filter(isUpper,strng)
    lowers = filter(isLower,strng)
    return uppers,lowers
于 2013-05-08T05:10:27.930 回答
1

不完全确定你在这里做什么,但你至少想做:

filter(isUpper,strng)
filter(isLower,strng)

代替:

filter(isUpper(x),strng)
filter(isLower(y),strng)

我相信你也想摆脱那些 2 for 循环。

从文档中:

请注意,如果 function 不是 None,filter(function, iterable) 等效于 [item for item in iterable if function(item)],如果 function 是 None,则等效于 [item for item in iterable if item]。

于 2013-05-08T05:08:48.370 回答
1

来自 python 文档(http://docs.python.org/2/library/functions.html#filter

filter(function, iterable)

filter(isUpper(x),strng)
filter(isLower(y),strng)

isUpper(x)计算结果为布尔值,而不是函数。

相反,写

filter(isUpper,strng)
filter(isLower,strng)
于 2013-05-08T05:06:27.813 回答
1

很难理解letterSplit应该做什么。以下是您可以使用的方法filter

Docstring:
filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true.  If
function is None, return the items that are true.  If sequence is a tuple
or string, return the same type, else return a list.

In [1]: s = "Having written the above functions, I now need to write the function letterSplit using only isUpper, isLower and the higher-order filter function."

In [2]: def isLower(x):
   ...:     if ord(x)>96 and ord(x)<123:
   ...:         return True
   ...:     else:
   ...:         return False
   ...:     

In [3]: filter(isLower, s)
Out[3]: 'avingwrittentheabovefunctionsnowneedtowritethefunctionletterplitusingonlyispperisowerandthehigherorderfilterfunction'
于 2013-05-08T05:07:48.027 回答