繁体   English   中英

检索列表中的字典项? python

[英]Retrieve dictionary items inside a list ? python

我是新手,自学 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 ")

上面的output如下:

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)

感谢对此的任何指导

我遇到了我想要的答案,谢谢。

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM