how to group own any one common in the list?

Ho Yeung Lee jobmattcon at
Tue Mar 28 05:37:35 EDT 2017

aaa = ["a","ab","c","b","bc"]

def similar(aa):
    similarset = []
    for ii in range(0,len(aa)):
        for jj in range(ii,len(aa)):
            if ii <> jj:
                if (aa[ii] in aa[jj]) or (aa[jj] in aa[ii]):
                    print(aa[ii] + " is similar with " + aa[jj])
    return similarset

print similar(aaa)

do not know how to edit following code to group own any one common in the list?

import itertools
bb = similar(aaa)
from operator import itemgetter
[list(g) for _,g in itertools.groupby(sorted(bb),(itemgetter(0,1) and itemgetter(1,0)))]

Expected result:

group 1 [['a', 'ab'], [['ab', 'b']]
group 2 [['b', 'bc'], [['c', 'bc']]

More information about the Python-list mailing list