简体   繁体   中英

python function for creating pairs based on ordered items in list

dat = [(1,"hello"),(1,"how are you?"),(2,"I am doing well, thanks!"),
       (1,"Do anything fun this weekend?"),(2,"I mostly slept"),
       (2,"but I also played games"),(1,"That sounds fun")]

Using python, I am trying to create a conversation pair dataset where each pair in the set will have speaker 1 and the subsequent response from speaker 2. With the example dat above, I would need to iterate through the list and 1) merge sentences from the same speaker if they are subsequent and 2) create the pair as below example;

Output:

((1,"hello how are you"),(2,"I am doing well, thanks!"))
((1,"Do anything fun this weekend?",(2,"I mostly slept but I also played games"))
((1,"That sounds fun"),(2,None))

How do I write a function that takes in sequential data like this to create conversation pairs?

conv = []
ls, lm = dat[0]
for s, m in dat[1:]:
    if s == ls:
        lm += ' ' + m
    else:
        conv.append((ls, lm))
        ls, lm = s, m
else:
    conv.append((ls, lm))
    if conv[-1][0] == 1:
        conv.append((2, None))
output = tuple([(conv[i], conv[i+1]) for i in range(0,len(conv) - 1, 2)])

Output:

[((1, 'hello how are you?'), (2, 'I am doing well, thanks!')), ((1, 'Do anything fun this weekend?'), (2, 'I mostly slept but I also played games')), ((1, 'That sounds fun'), (2, None))]

use this snippet of code, hope this solves your purpose

def combine_speaker_lines(id_part_iter):
    conversation = []
    last_speaker, part = id_part_iter[0]
    for speaker, fragment in id_part_iter[1:]:
        if speaker != last_speaker:
            conversation.append((last_speaker, part.lstrip()))
            part = ''
        part += ' ' + fragment
        last_speaker = speaker
    conversation.append((speaker, part.lstrip()))
    return conversation
    return conversation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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