美文网首页
列表中的各子列表元素之间的级联合并

列表中的各子列表元素之间的级联合并

作者: Babyzpj | 来源:发表于2018-12-19 19:28 被阅读0次
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']]

相关文章

网友评论

      本文标题:列表中的各子列表元素之间的级联合并

      本文链接:https://www.haomeiwen.com/subject/pskskqtx.html