【问题标题】:JSON File: Count the Full Number of Words with PythonJSON 文件:用 Python 计算全字数
【发布时间】:2020-05-05 14:20:00
【问题描述】:

对于当前的研究项目,我计划测量 JSON 文件中唯一单词的相对出现次数。目前,我有一个指示文件中唯一单词的数量及其相应的出现次数(例如"technology":"325"),但我仍然缺乏一个完整的字数统计方法。

我用于完整字数统计的代码(total = sum(d[key])) 会产生以下通知。我已经检查了一些类似问题的解决方案,但尚未找到适用的答案。有什么聪明的方法可以解决这个问题吗?

total = sum(d[key]) - TypeError: 'int' object is not iterable

对应的代码部分如下所示:

# Create an empty dictionary
d = dict()

# processing:
for row in data:
    line = row['Text Main']
    # Remove the leading spaces and newline character
    line = line.strip()

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

    # Remove the punctuation marks from the line
    line = line.translate(line.maketrans("", "", string.punctuation))

    # Split the line into words
    words = line.split(" ")

    # 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

# Print the contents of dictionary
for key in list(d.keys()):
    print(key, ":", d[key])

    # Count the total number of words
    total = sum(d[key])
    print(total)

【问题讨论】:

  • total = sum(d.values())
  • 非常感谢 - 这就是我想要的。 :)

标签: python json text nlp


【解决方案1】:

https://docs.python.org/3/library/functions.html#sum

您正在尝试sum(iterable, /, start=0) 一个整数。这没有任何意义,因为sum 意味着在可迭代对象上调用。对可迭代的简要解释是,您可以在其上使用 for 循环。例如,list

您可以通过以下两种方式之一修改您的 # Print the contents of dictionary 循环:

# Print the contents of dictionary
total = 0
for key in list(d.keys()):
    print(key, ":", d[key])

    # Count the total number of words
    total += d[key]
    print(total)
print("Actual total: ," total)

或者,更简洁:

# Print the contents of dictionary
for key in list(d.keys()):
    print(key, ":", d[key])

# Get the total word count
total = sum(d.values())

【讨论】:

  • 感谢您的意见。最后一点:计算d[key] / total 我再次收到通知TypeError: unsupported operand type(s) for /: 'dict' and 'int'。我假设我必须将 d[key] 转换为整数?
  • 有趣。 d[key] 应该是 inttotal。当你print(d[key], total) 时会发生什么?
  • 找到了解决方案:代码的最后一行必须包含迭代,并且必须修改如下:writer.writerows([key, d[key], d[key] / total] for key in list(d.keys()))。通过这个调整,代码运行良好。再次感谢您的支持:)
【解决方案2】:

python 的内置 sum 函数将 iterable 作为参数,但您试图将单个数字传递给它。你的代码相当于

total = sum(1)

但是 sum 函数需要添加一些可迭代的东西来计算 sum 。例如

sum([1,2,3,4,5,6,7])

如果你想计算总字数,你可以试试:

sum(d.values())

【讨论】:

  • 非常感谢 - 这非常有帮助。最后一点:计算d[key] / total 我再次收到通知TypeError: unsupported operand type(s) for /: 'dict' and 'int'。我假设我必须将 d[key] 转换为整数?
【解决方案3】:
d=dict()
d['A']=1
d['B']=2
d['C']=3
total = sum(d.values())
print total

for key in list(d.keys()):
    print(key, ":", d[key], float(d[key])/total)

    #Count the total number of words

d[key] 是单个 int d.values() 是一个列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多