检索列表中的字典项?蟒蛇

时间:2020-04-06 02:10:46

标签: python dictionary

我是新手,自学python。我已经研究了一天如何进行以下操作,请提供指导。

我正在尝试从中检索分钟,小时,秒等的值,但是我相信它的字典包装在一个列表中?让我感到困惑

[Slot(slotName='Duration', entity='snips/duration', rawValue='for 3 minutes', value={'kind': 'Duration', 'years': 0, 'quarters': 0, 'months': 0, 'weeks': 0, 'days': 0, 'hours': 0, 'minutes': 3, 'seconds': 0, 'precision': 'Exact'}, range={'start': 13, 'end': 26}, alternatives=[], confidenceScore=None)]

我的代码段如下:

        if 'Duration' in session.slotsAsObjects:
            print("running duration code")
            print("")

            slotObjects = session.slotsAsObjects

            print("slotObjects type is ", type(slotObjects))
            print("")
            print("slotObjects returns : ", slotObjects)
            print(slotObjects.get('Duration'))
            print("")

            slot = slotObjects.get('Duration', list())

            print("slot type is :", type(slot))
            print("")
            print("Slot value is ", slot)
            print("")

            if slot:
                minutes = slot[0]
                print("Below is what print(slot[0]) returns ")
                print(minutes)

            else:
                print("nothing to see here ")

,上面的输出如下:

running duration code

slotObjects type is  <class 'collections.defaultdict'>

slotObjects returns :  defaultdict(<class 'list'>, {'AddReminder': [Slot(slotName='AddReminder', entity='AddReminder', rawValue='reminder', value={'kind': 'Custom', 'value': 'reminder'}, range={'start': 4, 'end': 12}, alternatives=[], confidenceScore=None)], 'Duration': [Slot(slotName='Duration', entity='snips/duration', rawValue='for 3 minutes', value={'kind': 'Duration', 'years': 0, 'quarters': 0, 'months': 0, 'weeks': 0, 'days': 0, 'hours': 0, 'minutes': 3, 'seconds': 0, 'precision': 'Exact'}, range={'start': 13, 'end': 26}, alternatives=[], confidenceScore=None)]})
[Slot(slotName='Duration', entity='snips/duration', rawValue='for 3 minutes', value={'kind': 'Duration', 'years': 0, 'quarters': 0, 'months': 0, 'weeks': 0, 'days': 0, 'hours': 0, 'minutes': 3, 'seconds': 0, 'precision': 'Exact'}, range={'start': 13, 'end': 26}, alternatives=[], confidenceScore=None)]

slot type is : <class 'list'>

Slot value is  [Slot(slotName='Duration', entity='snips/duration', rawValue='for 3 minutes', value={'kind': 'Duration', 'years': 0, 'quarters': 0, 'months': 0, 'weeks': 0, 'days': 0, 'hours': 0, 'minutes': 3, 'seconds': 0, 'precision': 'Exact'}, range={'start': 13, 'end': 26}, alternatives=[], confidenceScore=None)]

Below is what print(slot[0]) returns 
Slot(slotName='Duration', entity='snips/duration', rawValue='for 3 minutes', value={'kind': 'Duration', 'years': 0, 'quarters': 0, 'months': 0, 'weeks': 0, 'days': 0, 'hours': 0, 'minutes': 3, 'seconds': 0, 'precision': 'Exact'}, range={'start': 13, 'end': 26}, alternatives=[], confidenceScore=None)

赞赏对此的任何指导

1 个答案:

答案 0 :(得分:0)

我已经找到了我要感谢的答案。

@staticmethod
  def getDuration(session: DialogSession) -> int:
    slots = session.slotsAsObjects
    duration = 0
    if 'Duration' in slots and slots['Duration'][0].entity == 'snips/duration':
      with suppress(TypeError, KeyError):
        values = slots['Duration'][0].value
        duration += values['seconds']
        duration += values['minutes'] * 60
        duration += values['hours'] * 60 * 60
        duration += values['days'] * 24 * 60 * 60
        duration += values['weeks'] * 7 * 24 * 60 * 60
        duration += values['months'] * 4 * 7 * 24 * 60 * 60

    return duration