【问题标题】:Python: TypeError: unhashable type: 'list' indices must be integersPython:TypeError:不可散列的类型:“列表”索引必须是整数
【发布时间】:2020-05-13 07:29:50
【问题描述】:

对于当前的一个研究项目,我计划在 Python/Pandas 的基础上,在预先定义的时间范围内读取 JSON 对象“Main_Text”。但是,在计算唯一词时,代码会为行 if word in d: 产生错误 TypeError: unhashable type: 'list' indices must be integers

我已经通过故障排除线程,除其他外,尝试将事物设置为元组(如某些线程所建议的那样),这已经克服了错误但导致输出为空。是否有任何有用的调整来完成这项工作?

JSON 文件具有以下结构:

[
{"No":"121","Stock Symbol":"A","Date":"05/11/2017","Text Main":"Sample text"}
]

相关的代码摘录如下:

import string
import json
import csv

import pandas as pd
import datetime

import numpy as np


# Loading and reading dataset
file = open("Glassdoor_A.json", "r")
data = json.load(file)
df = pd.json_normalize(data)
df['Date'] = pd.to_datetime(df['Date'])


# Create an empty dictionary
d = dict()


# Filtering by date
start_date = "01/01/2009"
end_date = "01/01/2015"

after_start_date = df["Date"] >= start_date
before_end_date = df["Date"] <= end_date

between_two_dates = after_start_date & before_end_date
filtered_dates = df.loc[between_two_dates]

print(filtered_dates)


# Processing
for row in filtered_dates:
    line = list(filtered_dates['Text Main'])
    # Remove the leading spaces and newline character

    line = [val.strip() for val in line]

    # Convert the characters in line to
    # lowercase to avoid case mismatch
    line = [val.lower() for val in line]

    # Remove the punctuation marks from the line
    line = [val.translate(val.maketrans("", "", string.punctuation)) for val in line]

    # Split the line into words
    words = [val.split(" ") for val in line]

    # Iterate over each word in line
    for word in words:
        # Check if the word is already in dictionary
        if word in d:
            # Increment count of word by 1
            d[word] = d[word] + 1
        else:
            # Add the word to dictionary with count 1
            d[word] = 1


【问题讨论】:

    标签: python pandas dataframe nlp


    【解决方案1】:
    if word in d.keys() 
    

    因为 'd' 是字典,所以你不能这样做:

    if word in d: # does not work like this to check if something is present in a dictionary
    

    我已对您的 for 循环进行了必要的更改:

    for row in filtered_dates:
        line = row['Text Main']
        # Remove the leading spaces and newline character
        line = line.split(' ')
        line = [val.strip() for val in line]
    
        # Convert the characters in line to
        # lowercase to avoid case mismatch
        line = [val.lower() for val in line]
    
        # Remove the punctuation marks from the line
        line = [val.translate(val.maketrans("", "", string.punctuation)) for val in line]
        print(line)
        # Split the line into words
        # words = [val.split(" ") for val in line]
        # print(words)
        # Iterate over each word in line
        for word in line:
            # Check if the word is already in dictionary
            if word in d.keys():
                # Increment count of word by 1
                d[word] = d[word] + 1
            else:
                # Add the word to dictionary with count 1
                d[word] = 1
    
    print(d)
    

    【讨论】:

    • 在 'words' 的 'for' 循环之前,如果你打印你得到的单词:[['s'], ['a'], ['m'], ['p'] , ['l'], ['e'], [''], ['t'], ['e'], ['x'], ['t'], [''], ['1 ']]
    • 您的第二个陈述是错误的。 if word in d 转换为 if word in d.keys()。您正在检查字典键中是否存在可散列类型。
    • OP,错误是您没有输入您认为的密钥。请重新检查您的 for 循环并打印以确保您的密钥是您所期望的。看来您的密钥是一个列表。
    • @M.S. => 我已经对你的 for 循环进行了更改,最终 print(d) 输出 ` {'text': 2, '1': 1, 'sample': 2, '2': 1} `
    • 如果您在之前的代码中为 'line' 和 'words' 变量添加打印语句,您就会明白哪里出错了。你的'线'看起来像这样:['S','a','m','p','l','e','','t','e','x','t ', ' ', '1'] 在循环本身的第一行之后。
    猜你喜欢
    • 2015-09-02
    • 2014-11-16
    • 2017-10-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多