python函数,用于根据列表中的有序项创建对
使用python,我试图创建一个对话对数据集,其中集合中的每一对都有speaker 1和speaker 2的后续响应。 对于上面的示例dat,我需要遍历列表,1)合并来自同一个说话人的句子(如果它们是后续的),2)按照下面的示例创建一对句子 输出:python函数,用于根据列表中的有序项创建对,python,list,function,Python,List,Function,使用python,我试图创建一个对话对数据集,其中集合中的每一对都有speaker 1和speaker 2的后续响应。 对于上面的示例dat,我需要遍历列表,1)合并来自同一个说话人的句子(如果它们是后续的),2)按照下面的示例创建一对句子 输出: dat = [(1,"hello"),(1,"how are you?"),(2,"I am doing well, thanks!"), (1,"Do anything fun this weekend?"),(2,"I mostly
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")]
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)])
我如何编写一个函数,它接收像这样的顺序数据来创建对话对
((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))
输出:
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")]
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)])
使用这段代码,希望能解决您的问题