Ask Your Question

Revision history [back]

Name the list:

sage: L = [ [1, 2, 3, 4], [1, 3, 1, 3], [4, 1, 4, 1], [1, 3, 2, 1], [4, 1, 2, 4] ]

Generate all desirable sublists:

sage: [list(map(list,S)) for S in Subsets(map(tuple,L)) if all(any(a == b for a, b in zip(A,B)) for A, B in Combinations(S, 2))]
[[],
 [[1, 2, 3, 4]],
 [[1, 3, 1, 3]],
 [[4, 1, 4, 1]],
 [[1, 3, 2, 1]],
 [[4, 1, 2, 4]],
 [[1, 3, 1, 3], [1, 2, 3, 4]],
 [[1, 3, 2, 1], [1, 2, 3, 4]],
 [[4, 1, 2, 4], [1, 2, 3, 4]],
 [[1, 3, 2, 1], [1, 3, 1, 3]],
 [[1, 3, 2, 1], [4, 1, 4, 1]],
 [[4, 1, 2, 4], [4, 1, 4, 1]],
 [[1, 3, 2, 1], [4, 1, 2, 4]],
 [[1, 3, 2, 1], [1, 3, 1, 3], [1, 2, 3, 4]],
 [[1, 3, 2, 1], [4, 1, 2, 4], [1, 2, 3, 4]],
 [[1, 3, 2, 1], [4, 1, 2, 4], [4, 1, 4, 1]]]

Or just some longest one:

sage: max((list(map(list,S)) for S in Subsets(map(tuple,L)) if all(any(a == b for a, b in zip(A,B)) for A, B in Combinations(S, 2))), key=lambda C: len(C))
[[1, 3, 2, 1], [1, 3, 1, 3], [1, 2, 3, 4]]