1 | initial version |
Give this one a try:
def find_common_lists(lists): common_lists = [] for i in range(len(lists)): for j in range(i+1, len(lists)): if set(lists[i]) & set(lists[j]): common_lists.append(lists[i]) common_lists.append(lists[j]) return common_lists def find_largest_common_lists(lists): largest_common_lists = [] for i in range(len(lists)): for j in range(i+1, len(lists)): if set(lists[i]) & set(lists[j]): common_lists = [lists[i], lists[j]] for k in range(j+1, len(lists)): if set(lists[k]) & set(common_lists[0]): common_lists.append(lists[k]) if len(common_lists) > len(largest_common_lists): largest_common_lists = common_lists return largest_common_lists # Test the find_common_lists function lists = [ [1, 2, 3, 4], [1, 3, 1, 3], [4, 1, 4, 1], [1, 3, 2, 1], [4, 1, 2, 4] ] print(find_common_lists(lists)) # Test the find_largest_common_lists function lists = [ [1, 2, 3, 4], [1, 3, 1, 3], [4, 1, 4, 1], [1, 3, 2, 1], [4, 1, 2, 4] ] print(find_largest_common_lists(lists))
2 | No.2 Revision |
Give this one a try:
def find_common_lists(lists): common_lists = [] for i in range(len(lists)): for j in range(i+1, len(lists)): for k in range(len(lists[i])): ifset(lists[i]) & set(lists[j]):lists[i][k] == lists[j][k]: common_lists.append(lists[i]) common_lists.append(lists[j]) break return common_lists def find_largest_common_lists(lists): largest_common_lists = [] for i in range(len(lists)): for j in range(i+1, len(lists)): for k in range(len(lists[i])): ifset(lists[i]) & set(lists[j]):lists[i][k] == lists[j][k]: common_lists = [lists[i], lists[j]]for kfor l in range(j+1, len(lists)): for m in range(len(lists[l])): ifset(lists[k]) & set(common_lists[0]): common_lists.append(lists[k])lists[l][m] == lists[i][m]: common_lists.append(lists[l]) break if len(common_lists) > len(largest_common_lists): largest_common_lists = common_lists return largest_common_lists # Test the find_common_lists function lists = [ [1, 2, 3, 4], [1, 3, 1, 3], [4, 1, 4, 1], [1, 3, 2, 1], [4, 1, 2, 4] ] print(find_common_lists(lists)) # Test the find_largest_common_lists function lists = [lists = [ [1, 2, 3, 4], [1, 3, 1, 3], [4, 1, 4, 1], [1, 3, 2, 1], [4, 1, 2, 4] ] print(find_largest_common_lists(lists))