【问题标题】:AttributeError: 'builtin_function_or_method' object has no attribute 'replace'AttributeError: 'builtin_function_or_method' 对象没有属性 'replace'
【发布时间】:2014-09-02 17:29:47
【问题描述】:

当我尝试在我的程序中使用它时,它说有一个属性错误

'builtin_function_or_method' object has no attribute 'replace'

但我不明白为什么。

def verify_anagrams(first, second):
    first=first.lower
    second=second.lower
    first=first.replace(' ','')
    second=second.replace(' ','')
    a='abcdefghijklmnopqrstuvwxyz'
    b=len(first)
    e=0
    for i in a:
        c=first.count(i)
        d=second.count(i)
        if c==d:
            e+=1
    return b==e

【问题讨论】:

    标签: python string attributeerror


    【解决方案1】:

    您需要调用 str.lower 方法,方法是在其后放置 ():

    first=first.lower()
    second=second.lower()
    

    否则,first 和 second 将被分配给函数对象本身:

    >>> first = "ABCDE"
    >>> first = first.lower
    >>> first
    <built-in method lower of str object at 0x01C765A0>
    >>>
    >>> first = "ABCDE"
    >>> first = first.lower()
    >>> first
    'abcde'
    >>>
    

    【讨论】:

    • 非常感谢,我没找对地方!
    猜你喜欢
    • 2018-11-29
    • 2018-03-27
    • 2020-11-27
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多