[Python] TypeError: list indices must be integers or slices, not str when using nested...

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm creating an inverted index of text files stored locally using nested dictionaries. An abstract structure of the inverted index is below (the values are integer numbers). In any word value of key '0' is the idf and value of key '1' is the tf.

    inverted_index={'word1':{'0':idf_value, '1': 2 , 'filename1': frequency_value, 'filename2': frequency_value},'word2':{'0':idf_value, '1': 2, 'filename1': frequency_value, 'filename2': frequency_value}}


    And this is the code:

    import textract, math, os
    docs=[]
    #Read the files and store them in docs
    folder = os.listdir("./input/")
    for file in folder:
    if file.endswith("txt"):
    docs.append ([file,textract.process("./input/"+file)])

    inverted_index={}
    for doc in docs:
    words=doc[1].decode()
    words=words.split(" ")

    #loop through and build the inverted index
    for word in words:
    temp={}
    #to remove initial white space
    if (word == " ") or (word==""):
    continue
    if word not in inverted_index:
    temp[doc[0]]=1
    temp['0']=0 #idf
    temp['1']=1 #tf
    inverted_index[word]=temp
    else:
    if doc[0] not in inverted_index[word].keys():
    inverted_index[word][doc[0]]=1
    inverted_index[word]['1']=inverted_index[word]['1']+1
    else:
    inverted_index[word][doc[0]]=inverted_index[word][doc[0]]+1

    # to sort and print values with calculating the the tf and idf on the fly
    for key, value in sorted(inverted_index.items()): # to sort words alphabitically
    inverted_index[key]=sorted(inverted_index[key]) # to sort the filenames where the word occured.
    inverted_index[key]['0']=math.log2(len(docs)/value['1']) # the error in this line
    print(key, value)


    but I get this error in the second last line:

    Traceback (most recent call last):
    File "aaaa.py", line 34, in <module>
    inverted_index[key]['0']=math.log2(len(docs)/value['1'])
    TypeError: list indices must be integers or slices, not str


    How can I fix this bug?

    Continue reading...

Compartilhe esta Página