简体   繁体   中英

Python error: AttributeError: 'int' object has no attribute 'append'

So I've looked through similar questions, and I'm still getting the same problem and can't figure it out. For this programming assignment, I'm creating a simplified version of lexical analysis for a small subset of the Clite lexicon. I'm extracting tokens from an input file, outputting the results of my analysis. I'm creating a symbol table for the identifiers founds using a dictionary. When I find the same identifiers on different lines, I need to append to the symbol table the line that it was found on. For example, I find the identifier "number18" on line 2 and again on line 7. So the symbol table would need to go from {number18: 2} to {number18: 2,7}

The problem comes when I try to append the new line number onto a current dictionary entry. I get the error as I posted in the question title. Here's my code thus far

y = 0
s2 = ()
stable = dict()

for line in open("Sample.txt","r"):
    x1 = ''
    for char in line:
    if char.isalpha():
        x1 = x1 + char
    elif char.isdigit():
        x1 = x1 + char
    elif char == '.':
        x1 = x1 + char
    elif x1 != '':
        break

    #print (x1)    
    if (x1 == "for" or x1 == "bool" or x1 == "char" or x1 == "else" or x1 == "false" or x1 == "float" or x1 == "if" or x1 == "int" or x1 == "main" or x1 == "true" or x1 == "while"):
        s2=(y,"Keyword",x1)
    elif x1.isidentifier():
        s2=(y,"Identifier",x1)
    if x1 in stable.keys():
        stable[x1].append(y)
    else:
        stable[x1]=y


    elif x1.isdigit():
        s2=(y,"Int",x1)
    else:
        s2=(y,"Float",x1)
    print (s2)
    y=y+1

print (stable)

You first set your dict values to be an int :

    stable[x1]=y

but then you later on you try to treat it as if it is a list :

    stable[x1].append(y)

Start out with a list containing your first int instead:

    stable[x1]=[y]

and the .append() will work.

Alternatively, you could use a defaultdict :

stable = defaultdict(list)

and then append at will without needing to test if the key is already there:

    stable[x1].append(y)  # No need to do `if x1 in stable`.
elif x1.isidentifier():
    s2=(y,"Identifier",x1)
    if x1 in stable.keys():
        stable[x1].append(y)
    else:
        stable[x1]=y

In your else part above, you are adding integer first time. So when you use append next time, you would get that error.

Rather wrap your integer y in a list [y] , when you add your value first time to your dict

    else:
        stable[x1]=[y]

Well, you already know from @Martijn's answer that using a defaultdict will be better option here, as then you don't need to check for containment of key .

But, still relating to the way you are checking for the key in dict: -

if x1 in stable.keys():

You don't need to use stable.keys() , just use stable , that will check in keys only.

if x1 in stable:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM