[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: how to group own any one common in the list?
From: Ho Yeung Lee <jobmattcon () gmail ! com>
Date: 2017-03-28 9:37:35
Message-ID: 3bf09079-574d-4582-b93e-4338804a2043 () googlegroups ! com
[Download RAW message or body]
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:
print("("+str(ii)+","+str(jj)+")")
if (aa[ii] in aa[jj]) or (aa[jj] in aa[ii]):
print(aa[ii] + " is similar with " + aa[jj])
similarset.append([aa[ii],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']]
--
https://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic