[英]list comprehension within list of tuple
my problem is something like this:我的问题是这样的:
a=[(0,0,'customer',["Hi, I'm user"]),
(0,1,'agent',['Hi Welcome']),
(0,2,'customer',["i would like to know"]),
(0, 3, 'agent', ['Yes']),
(0, 3, 'agent', ['Only credit']),
(0, 4, 'customer', ['oic...']),
(0, 4, 'customer', ['sub line?']),
(0, 4, 'customer', ['is it?']),
(0, 5, 'agent', ['no subline']),
(0, 6, 'customer', ['oic...']),
(0, 6, 'customer', ['by']),
(0, 6, 'customer', ['bye'])]
Need to convert to
a=[(0,0,'customer',["Hi, I'm user"]),
(0,1,'agent',['Hi Welcome']),
(0, 2,'customer',["i would like to know"]),
(0, 3, 'agent', ['Yes','Only credit']),
(0, 4, 'customer', ['oic...','sub line?','is it?']),
(0, 5, 'agent', ['no subline']),
(0, 6, 'customer', ['oic...','by','bye'])]
I want to merge the list of responses based on the speaker (agent/customer).我想根据发言人(代理/客户)合并响应列表。 cant find quick logic... any help here?
找不到快速逻辑...这里有什么帮助吗?
You can't change it in_place as it's a tuple, so make another copy and check for pre-existing values to append to the list.您不能在 in_place 更改它,因为它是一个元组,因此制作另一个副本并检查 append 到列表中的预先存在的值。
a=[(0,0,'customer',["Hi, I'm user"]),
(0,1,'agent',['Hi Welcome']),
(0,2,'customer',["i would like to know"]),
(0, 3, 'agent', ['Yes']),
(0, 3, 'agent', ['Only credit']),
(0, 4, 'customer', ['oic...']),
(0, 4, 'customer', ['sub line?']),
(0, 4, 'customer', ['is it?']),
(0, 5, 'agent', ['no subline']),
(0, 6, 'customer', ['oic...']),
(0, 6, 'customer', ['by']),
(0, 6, 'customer', ['bye'])]
b = []
str2idx = []
idx = 0
for p in a:
if p[:3] in str2idx:
b[str2idx.index(p[:3])][3].append(p[3][0])
else:
b.append(p)
str2idx.append(p[:3])
idx += 1
print(b)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.