我有两个带有字符串(list_a
和list_b
)的嵌套列表,详细信息如下:
list_a = [
('shop1', 'stand1', 'shelf1', 'fruit1'),
('shop1', 'stand1', 'shelf2', 'fruit2'),
('shop1', 'stand1', 'shelf3', 'fruit3'),
('shop1', 'stand2', 'shelf1', 'fruit1'),
('shop1', 'stand2', 'shelf2', 'fruit2'),
('shop1', 'stand2', 'shelf3', 'fruit3'),
('shop2', 'stand3', 'shelf1', 'fruit1'),
('shop2', 'stand3', 'shelf2', 'fruit2'),
('shop2', 'stand3', 'shelf3', 'fruit3')
]
list_b = [
('shop1', 'stand1', 'shelf1', 'fruit1'),
('shop1', 'stand1', 'shelf2', 'fruit2'),
('shop1', 'stand1', 'shelf2', 'fruit2'),
('shop1', 'stand1', 'shelf3', 'fruit3'),
('shop1', 'stand1', 'shelf3', 'fruit3'),
('shop1', 'stand1', 'shelf3', 'fruit3'),
('shop1', 'stand2', 'shelf1', 'fruit1'),
('shop1', 'stand2', 'shelf1', 'fruit1'),
('shop1', 'stand2', 'shelf1', 'fruit1'),
('shop1', 'stand2', 'shelf2', 'fruit2'),
('shop1', 'stand2', 'shelf2', 'fruit2'),
('shop1', 'stand2', 'shelf2', 'fruit2'),
('shop1', 'stand2', 'shelf3', 'fruit3'),
('shop2', 'stand3', 'shelf1', 'fruit1'),
('shop2', 'stand3', 'shelf1', 'fruit1'),
('shop2', 'stand3', 'shelf2', 'fruit2'),
('shop2', 'stand3', 'shelf3', 'fruit3'),
('shop2', 'stand3', 'shelf3', 'fruit3'),
('shop2', 'stand3', 'shelf3', 'fruit3')
]
我想从list_b
in 中找到相同的行list_a
,计算“重复”行并将 list_a 与一个附加列(出现次数)合并为一个新列表,如下所示:
result_list = [
('shop1', 'stand1', 'shelf1', 'fruit1', 1),
('shop1', 'stand1', 'shelf2', 'fruit2', 2),
('shop1', 'stand1', 'shelf3', 'fruit3', 3),
('shop1', 'stand2', 'shelf1', 'fruit1', 3),
('shop1', 'stand2', 'shelf2', 'fruit2', 3),
('shop1', 'stand2', 'shelf3', 'fruit3', 1),
('shop2', 'stand3', 'shelf1', 'fruit1', 2),
('shop2', 'stand3', 'shelf2', 'fruit2', 1),
('shop2', 'stand3', 'shelf3', 'fruit3', 3)
]
有没有快速有效的方法来做到这一点?