I have this list:
lst =[['BOX_187_090_31', 'BOX_187_090_32', 'BOX_187_090_34', 'BOX_187_090_35', 'BOX_187_090_36', 'BOX_187_090_37',
'BOX_187_090_38', 'BOX_187_090_48', 'BOX_187_090_49', 'BOX_187_090_50', 'BOX_187_090_51', 'BOX_187_090_52',
'BOX_187_090_53', 'BOX_187_090_54', 'BOX_187_090_55', 'BOX_187_090_56', 'BOX_187_090_57', 'BOX_187_090_58',
'BOX_187_090_59', 'BOX_187_090_60'],
['BOX_187_090_33', 'BOX_187_090_39', 'BOX_187_090_40', 'BOX_187_090_41',
'BOX_187_090_42', 'BOX_187_090_43'],
['BOX_187_090_61', 'BOX_187_090_62'],
['BOX_187_090_39', 'BOX_187_090_40', 'BOX_187_090_41', 'BOX_187_090_42', 'BOX_187_090_43'],
['BOX_187_090_33', 'BOX_187_090_42', 'BOX_187_090_43'], ['BOX_187_090_33', 'BOX_187_090_01']]
I want to combine all sublists that have one or more elements overlap.
For example, the sublists ['BOX_187_090_33', 'BOX_187_090_01']
and ['BOX_187_090_33', 'BOX_187_090_42', 'BOX_187_090_43']
have overlap by the element 'BOX_187_090_33'. The merge will then look like: ['BOX_187_090_33', 'BOX_187_090_42', 'BOX_187_090_43', 'BOX_187_090_01']
.
Furthermore, in the case of three sublists it is also possible that two sublists have no overlap with eachother but they do have overlap with the third sublist, then they also have to be merged. For example:
['BOX_187_090_39', 'BOX_187_090_40', 'BOX_187_090_41', 'BOX_187_090_42', 'BOX_187_090_43'],
['BOX_187_090_33', 'BOX_187_090_42', 'BOX_187_090_43']
['BOX_187_090_33', 'BOX_187_090_01']
Becomes: ['BOX_187_090_33', 'BOX_187_090_39', 'BOX_187_090_40', 'BOX_187_090_41', 'BOX_187_090_42', 'BOX_187_090_43', 'BOX_187_090_01']
The final result for my list should be:
lst =[['BOX_187_090_31', 'BOX_187_090_32', 'BOX_187_090_34', 'BOX_187_090_35', 'BOX_187_090_36', 'BOX_187_090_37',
'BOX_187_090_38', 'BOX_187_090_48', 'BOX_187_090_49', 'BOX_187_090_50', 'BOX_187_090_51', 'BOX_187_090_52',
'BOX_187_090_53', 'BOX_187_090_54', 'BOX_187_090_55', 'BOX_187_090_56', 'BOX_187_090_57', 'BOX_187_090_58',
'BOX_187_090_59', 'BOX_187_090_60'],
['BOX_187_090_33', 'BOX_187_090_39', 'BOX_187_090_40', 'BOX_187_090_41',
'BOX_187_090_42', 'BOX_187_090_43', 'BOX_187_090_01'],
['BOX_187_090_61', 'BOX_187_090_62']]
Anyone has an idea how to fix this?
Regards,
Dante