Filter Anagram in Array in Python -
i'm trying go through array , delete elements aren't anagrams in python. here code wrote. logic seems fine can't seem it.
b = ['cat', 'dog', 'god', 'star', 'lap', 'act'] array=[] t=0 in b: while t<len(b): if ''.join(sorted(i))==''.join(sorted(b[t])): array.append(i) t+=1 print array
just minor tweaks existing code should work.
b = ['cat', 'dog', 'god', 'star', 'lap', 'act'] array = [] t = 0 i, value in enumerate(b): t = i+1 while t<len(b): if ''.join(sorted(value))==''.join(sorted(b[t])): array.extend([value, b[t]]) t+=1 print array ['cat', 'act', 'dog', 'god']
Comments
Post a Comment