Python 如何在同一数组中返回/输出JSON对象?

Python 如何在同一数组中返回/输出JSON对象?,python,json,Python,Json,这是我的程序,当发送文本输入字符串时,响应是每个单词及其出现次数(作为JSON对象返回): 现在,JSON输出如下所示: { "concordance": [ { "count": { "The": 1, "brown": 2, "fox": 1, "jumped": 1,

这是我的程序,当发送文本输入字符串时,响应是每个单词及其出现次数(作为JSON对象返回):

现在,JSON输出如下所示:

{
  "concordance": [
    {
      "count": {
        "The": 1,
        "brown": 2,
        "fox": 1,
        "jumped": 1,
        "log.": 1,
        "over": 1,
        "the": 1
      },
      "token": [
        "The",
        "brown",
        "fox",
        "jumped",
        "over",
        "the",
        "brown",
        "log."
      ]
    }
  ],
  "input": "The brown fox jumped over the brown log."
}
但是,我正在尝试将输出格式化为:

{
  "concordance": [
    {
      "token": "brown",
      "count": 2
    },
    {
      "token": "fox",
      "count": 1
    },
    {
      "token": "jumped",
      "count": 1
    },
    {
      "token": "log",
      "count": 1
    },
    {
      "token": "over",
      "count": 1
    },
    {
      "token": "the",
      "count": 1
    }
  ],
  "input": "The brown fox jumped over the brown log."
}
有人知道我如何更改代码,使其正确打印吗? 我不知道如何将列表中的每个单词分开,并将其与计数关联起来。
谢谢。

在列表中构建您的令牌计数词典列表

def get_concordance(body):  # noqa: E501
    # Documentation omitted for brevity
    input_text = body.decode('utf-8')
    split_string = input_text.split()

    def word_count():
        # counts = dict()
        counts = {}

        for word in split_string:
            if word in counts:
                counts[word] += 1
            else:
                counts[word] = 1

        return counts
    
    try:
        # {"the": 2, "brown": 1}
        counts = word_count()
        # This is the changed part.
        concordance = [{"count": counts[token], "token": token} for token in counts.keys()]
        response = {
            "concordance": concordance,
            "input": input_text,
        }
    except Exception as error:
        response = {
            "error": repr(error)
        }
    return response

下面是一种相对简单的方法,它使用类稍微简化一些事情(并去掉嵌套函数):

输出:

{'concordance':[{'token':'The','count':1},
{'token':'brown','count':1},
{'token':'fox','count':1},
{'token':'jumped','count':1},
{'token':'over','count':1},
{'token':'the','count':1},
{'token':'log','count':1}],
'输入':'棕色狐狸跳过了日志'}

我先发布了实际输出,然后才发布了所需的输出。您能发布示例输入吗示例输入是
棕色狐狸跳过了棕色日志。
输出格式正确,但返回的是重复的单词(即“棕色”)。我想列出一次单词“brown”及其当前计数。当前输出(具有重复的“brown”):
{“concordance”:[{“count”:1,“token”:“the”},{“count”:2,“token”:“brown”},{“count”:1,“token”:“fox”},{“count”:1,“token”:“hopped”},{“count”:1,“token”:“over”},{“count”:1,“token”:“the”},{“count”:2,“token”:“brown”},{“count”:1,“token”:“log”}],“input”:“棕色狐狸跳过棕色的日志”}
@HannahYoussef-通过迭代
计数
字典而不是拆分字符串来修复它。工作非常完美。非常感谢。
def get_concordance(body):  # noqa: E501
    # Documentation omitted for brevity
    input_text = body.decode('utf-8')
    split_string = input_text.split()

    def word_count():
        # counts = dict()
        counts = {}

        for word in split_string:
            if word in counts:
                counts[word] += 1
            else:
                counts[word] = 1

        return counts
    
    try:
        # {"the": 2, "brown": 1}
        counts = word_count()
        # This is the changed part.
        concordance = [{"count": counts[token], "token": token} for token in counts.keys()]
        response = {
            "concordance": concordance,
            "input": input_text,
        }
    except Exception as error:
        response = {
            "error": repr(error)
        }
    return response
inputs = b"The brown fox jumped over the brown log"
get_concordance(inputs)

{'concordance': [{'count': 1, 'token': 'The'},
  {'count': 2, 'token': 'brown'},
  {'count': 1, 'token': 'fox'},
  {'count': 1, 'token': 'jumped'},
  {'count': 1, 'token': 'over'},
  {'count': 1, 'token': 'the'},
  {'count': 1, 'token': 'log'}],
 'input': 'The brown fox jumped over the brown log'}
import collections
from pprint import pprint
#from swagger_server.models.result import Result  # noqa: E501


def get_concordance(body):  # noqa: E501
    """Calculate

    Post text to generate concordance # noqa: E501

    :param body: Text to be analyzed
    :type body: dict | bytes

    :rtype: Result
    """

    input_text = body.decode('utf-8')
    split_string = input_text.split()
    word_counts = collections.Counter(split_string)

    pairs = [{'token': token, 'count': count}
                for token, count in zip(split_string, word_counts.values())]
    response = {
        "concordance": pairs,
        "input": input_text
    }
    return response



if __name__ == '__main__':

    body = b'The brown fox jumped over the log'
    resp = get_concordance(body)
    pprint(resp, sort_dicts=False)