【问题标题】:Python “List” object is not callable?Python“列表”对象不可调用?
【发布时间】:2012-05-07 20:05:59
【问题描述】:

我正在尝试在我使用正则表达式和 dict() 的地方运行此代码。我需要将匹配的元素放入正确的列表中,但出现错误。TypeError: 'list' object is not callable。谁能告诉我我在这里做错了什么。

dir='newDSSP'
for tname in os.listdir(dir):
    file=dir+os.sep+tname
    ndfile=open(file)
    tname=dict()
    tname.setdefault('A',[[],[]])
    tname.setdefault('B',[[],[]])
    tname.setdefault('C',[[],[]])
    tname.setdefault('D',[[],[]])
    for ndline in ndfile:
        t=re.match(r'(\s+|\S+)+\t\w+\t(\w)\t(\w)\t(\w|\s)', ndline)
        k=t.group(2)
        if k =='A':

            tname['A'](0).append(t.group(3))<--- **#Error here**
            tname['A'](1).append(t.group(4))
        elif k =='B':

            tname['B'](0).append(t.group(3))
            tname['B'](1).append(t.group(4))
        elif k =='C':

            tname['C'](0).append(t.group(3))
            tname['C'](1).append(t.group(4))
        elif k =='D':

            tname['D'](0).append(t.group(3))
            tname['D'](1).append(t.group(4))
    ndfile.close()

【问题讨论】:

    标签: python


    【解决方案1】:

    你有

    tname['A'](0).append(t.group(3))
    

    tname['A'] 不是包含两个列表的列表吗?在这种情况下,你想要

    tname['A'][0].append(t.group(3))
    

    【讨论】:

    • 好的,我明白了,主人!谢谢你的帮助!
    【解决方案2】:

    x() 总是一个函数调用,所以像tname['C'](0) 这样的东西试图调用tname['C'] 作为一个带有参数0 的函数。也许您打算将方括号用于列表索引?

    【讨论】:

    • 好的,这很有帮助。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-10-04
    • 2016-05-26
    • 2012-12-06
    • 2021-03-09
    • 2017-03-27
    • 2011-09-02
    • 2014-11-30
    • 2017-02-19
    相关资源
    最近更新 更多