combine_list = [['ab', 'ad'], ['c', 'cf'], ['ad', 'cf'], ['av', 'df'], ['df']]
def getResult(combine_list):
'''
作用,大列表combine_list各个子列表之间的级联。只要子列表之间交集不为0,就将这两个子列表进行合并。
若何并后的列表和其它列表交集不为0,就继续合并,
'''
result = []
while(combine_list):
for i in combine_list:
if result==[]:
result.append(i)
else:
lenthL = [len(set(j)&set(i)) for j in result]
if sum(lenthL)==0:
result.append(i)
else:
FlagL =[1 if k>0 else 0 for k in lenthL]
Flagindex = FlagL.index(1)
result[Flagindex] = result[Flagindex]+i
combine_list.remove(i)
# 将合并后的子列表进行元素去重
result = [list(set(item)) for item in result]
return result
LLL = getResult(combine_list)
LLL
>>> [['c', 'ab', 'ad', 'cf'], ['av', 'df']]
网友评论