xiuxing
xiuxing

Reputation: 1

Python “List” object is not callable?

I am trying to run this code where I use regular expression and dict(). I need to put the matching elements into the right list,but I get the error.TypeError: 'list' object is not callable. Can anyone tell me what am I doing wrong here.

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()

Upvotes: 0

Views: 1417

Answers (2)

geekosaur
geekosaur

Reputation: 61457

x() is always a function call, so something like tname['C'](0) is trying to call tname['C'] as a function with parameter 0. Perhaps you intended square brackets for a list index?

Upvotes: 1

srgerg
srgerg

Reputation: 19339

You have

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

but isn't tname['A'] a list containing two lists? In that case, you want

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

Upvotes: 8

Related Questions