多个列表根据交集进行合并
作者:互联网
# -*- coding: utf-8 -*-#
# -------------------------------------------------------------------------------
# Name: 列表根据交集合并
# Author: yunhgu
# Date: 2022/2/22 15:16
# Description:
# -------------------------------------------------------------------------------
def merge_list_with_intersection(list_2d: list):
"""
:param list_2d:二维列表
:return:
"""
sets = {}
for lst in list_2d:
s = set(lst)
t = set()
for x in s:
if x in sets:
t.update(sets.get(x))
else:
sets[x] = s
for y in t:
sets[y] = s
s.update(t)
result_list = []
for s in sets.values():
if list(s) not in result_list:
result_list.append(list(s))
return result_list
if __name__ == '__main__':
data_list = [[1, 2, 3], [6, 8], [4, 5, 3], [7, 9, 1], [0, 12]]
print(merge_list_with_intersection(data_list))
标签:__,交集,合并,list,列表,2d,result,sets 来源: https://www.cnblogs.com/yunhgu/p/15923549.html