【问题标题】:Python Nested dictionary data extractionPython嵌套字典数据提取
【发布时间】:2021-02-12 19:26:36
【问题描述】:

我有一本字典如下:

{
  "result" : [{
      "conf" : 1.000000,
      "end" : 105.570000,
      "start" : 104.970000,
      "word" : "today"
    }, {
      "conf" : 1.000000,
      "end" : 106.320000,
      "start" : 106.080000,
      "word" : "yes"
    }, {
      "conf" : 1.000000,
      "end" : 106.590000,
      "start" : 106.320000,
      "word" : "sir"
    }],
  "text" : "today yes sir"
}

我想从结果中的内部字典中提取startword 变量。 我该怎么做呢?任何帮助将不胜感激。

【问题讨论】:

  • 显示你的代码到目前为止你做了什么?

标签: python dictionary


【解决方案1】:

鉴于您的字典:

my_dict = {
  "result" : [{
      "conf" : 1.000000,
      "end" : 105.570000,
      "start" : 104.970000,
      "word" : "today"
    }, {
      "conf" : 1.000000,
      "end" : 106.320000,
      "start" : 106.080000,
      "word" : "yes"
    }, {
      "conf" : 1.000000,
      "end" : 106.590000,
      "start" : 106.320000,
      "word" : "sir"
    }],
  "text" : "today yes sir"
}

那么

start = my_dict['result'][0]['start']
word = my_dict['result'][0]['word']

【讨论】:

    【解决方案2】:

    所以这是你给的字典:

    dict ={
       "result" : [{
           "conf" : 1.000000,
           "end" : 105.570000,
           "start" : 104.970000,
           "word" : "today"
         }, {
           "conf" : 1.000000,
           "end" : 106.320000,
           "start" : 106.080000,
           "word" : "yes"
        }, {
           "conf" : 1.000000,
           "end" : 106.590000,
           "start" : 106.320000,
           "word" : "sir"
         }],
       "text" : "today yes sir"
    }
    

    这可以获取所有“单词”和“开始”值:

    list = dict.get('result')
    
    for i in list:
         print(i.get("start"))
         print(i.get("word"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 2021-08-24
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2021-10-29
      • 2013-08-09
      • 2016-09-15
      相关资源
      最近更新 更多