在Python中,如何在解析数据的同时用一个键分隔字典?

2024-09-21 07:34:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我将以下信息存储在一个名为conversation_counter的字典中。由于项目经理的指示,我无法共享整个代码,但这是我的迭代

{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '1', 'agent_text': 'Hey welcome to Hatch realty, before we start our conversation, can I start by asking your name?', 'customer_input': '', 'customer_name': 'Brandon', 'customer_intention': 'sell', 'customer_property': 'house'}
{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '2', 'agent_text': 'Great, thanks David. Is there something I can help you with, or what brought you to our site?', 'customer_input': 'My name is David', 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '3', 'agent_text': 'Of course, when would you like to move?', 'customer_input': 'I want to buy a house', 'customer_name': 'Brandon', 'customer_intention': 'buy', 'customer_property': 'house'}
{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '4', 'agent_text': 'Do you have a specific property address?', 'customer_input': 'This summer', 'customer_name': 'Brandon', 'customer_intention': 'buy', 'customer_property': 'house'}
{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '5', 'agent_text': 'Can I get your email address and phone number so I can have someone reach out to you?', 'customer_input': "No I don't.", 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '1', 'agent_text': 'Hey welcome to Hatch realty, before we start our conversation, can I start by asking your name?', 'customer_input': '', 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '2', 'agent_text': 'Great, thanks Brandon. Is there something I can help you with, or what brought you to our site?', 'customer_input': 'My name is Brandon', 'customer_name': 'Brandon', 'customer_intention': '', 'customer_property': ''}
{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '3', 'agent_text': 'Of course, Do you need to be out by a certain date or is your timeframe open?', 'customer_input': 'I want to sell a house', 'customer_name': 'Brandon', 'customer_intention': 'sell', 'customer_property': 'house'}
{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '4', 'agent_text': 'Do you have a specific property address?', 'customer_input': "It's open", 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '5', 'agent_text': 'Can I get your email address and phone number so I can have someone reach out to you?', 'customer_input': "No I don't", 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}

如您所见,只有两个不同的对话ID(第一个值)。对话并不总是有5个计数(请参见对话计数器),因此我如何1)找到每个对话id的最高对话计数器,或者为每个对话id创建单独的字典

这是我到目前为止所拥有的,但它只包含一个有意义的字典,因为我很难更新字典,所以它会取消第一个字典

conversationID = ""
data = {
    "conversation_id": "",
    "customer_name": "",
    "customer_intention": "",
    "customer_property": ""
        }
for convo in conversation_counter:
    conversationID = convo['conversation_id']
    for conversations in conversationID:
        data["conversation_id"] = convo['conversation_id']
        if customer_name != "":
            data["customer_name"] = convo['customer_name']
        if customer_intention != "":
            data["customer_intention"] = convo['customer_intention']
        if customer_property != "":
            data["customer_property"] = convo['customer_property']

print(data)

输出正是我想要的,但只给我一个条目。我认为最简单的方法是找到最高的对话框计数器,但我不知道如何在带有单独条目的for循环中实现这一点

{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'customer_name': 'Brandon', 'customer_intention': 'sell', 'customer_property': 'house'}

Tags: totextnameyouidinputcounterproperty
1条回答
网友
1楼 · 发布于 2024-09-21 07:34:28

当您将其分解为以下步骤时,这会更容易:

  1. 将消息聚合到对话中
  2. 对对话进行排序
  3. 从每个人那里获取最后一条消息
from typing import Dict, List
import pprint

data: List[Dict[str, str]] = [
    {'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '1', 'agent_text': 'Hey welcome to Hatch realty, before we start our conversation, can I start by asking your name?', 'customer_input': '', 'customer_name': 'Brandon', 'customer_intention': 'sell', 'customer_property': 'house'}
   ,{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '2', 'agent_text': 'Great, thanks David. Is there something I can help you with, or what brought you to our site?', 'customer_input': 'My name is David', 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
   ,{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '3', 'agent_text': 'Of course, when would you like to move?', 'customer_input': 'I want to buy a house', 'customer_name': 'Brandon', 'customer_intention': 'buy', 'customer_property': 'house'}
   ,{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '4', 'agent_text': 'Do you have a specific property address?', 'customer_input': 'This summer', 'customer_name': 'Brandon', 'customer_intention': 'buy', 'customer_property': 'house'}
   ,{'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6', 'dialog_counter': '5', 'agent_text': 'Can I get your email address and phone number so I can have someone reach out to you?', 'customer_input': "No I don't.", 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
   ,{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '1', 'agent_text': 'Hey welcome to Hatch realty, before we start our conversation, can I start by asking your name?', 'customer_input': '', 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
   ,{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '2', 'agent_text': 'Great, thanks Brandon. Is there something I can help you with, or what brought you to our site?', 'customer_input': 'My name is Brandon', 'customer_name': 'Brandon', 'customer_intention': '', 'customer_property': ''}
   ,{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '3', 'agent_text': 'Of course, Do you need to be out by a certain date or is your timeframe open?', 'customer_input': 'I want to sell a house', 'customer_name': 'Brandon', 'customer_intention': 'sell', 'customer_property': 'house'}
   ,{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '4', 'agent_text': 'Do you have a specific property address?', 'customer_input': "It's open", 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
   ,{'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee', 'dialog_counter': '5', 'agent_text': 'Can I get your email address and phone number so I can have someone reach out to you?', 'customer_input': "No I don't", 'customer_name': 'David', 'customer_intention': 'buy', 'customer_property': 'house'}
]

conversations:Dict[str, List[Dict[str, str]]] = dict() # typing indicator just for IDE nice-to-haves
# group conversations by conversation id
for message in data:
    cid = message.get('conversation_id')
    if not cid:
        continue
    if cid not in conversations:
        conversations[cid] = list()
    conversations[cid].append(message)

# sort each conversation by message dialog counter
conversations = {
    cid: sorted(conversation, key=(lambda message:message.get('dialog_counter')))
    for cid, conversation in conversations.items()
}

# get the last message in each conversation
last_messages = [conversation[-1] for conversation in conversations.values()]

pprint.pprint(last_messages)

我忘了输出:

[{'agent_text': 'Can I get your email address and phone number so I can have '
                'someone reach out to you?',
  'conversation_id': '4850dd66-05b9-43e9-b546-e4976c9c29b6',
  'customer_input': "No I don't.",
  'customer_intention': 'buy',
  'customer_name': 'David',
  'customer_property': 'house',
  'dialog_counter': '5'},
 {'agent_text': 'Can I get your email address and phone number so I can have '
                'someone reach out to you?',
  'conversation_id': 'dbec6faa-16cb-416a-8653-ffc36174ecee',
  'customer_input': "No I don't",
  'customer_intention': 'buy',
  'customer_name': 'David',
  'customer_property': 'house',
  'dialog_counter': '5'}]

相关问题 更多 >

    热门问题