1

I have an array like

sets=[ [ ['a', 'c'] , ['a', 'e'] ] , [ ['b', 'c'] , ['b', 'e'] ] , [ ['a','z'] ] ]

I wanted the reduced dimension of list and remove common element in the innerlists

My expected output is

[['a','c','e'] , ['b','c','e'] , ['a','z'] ]
miradulo
  • 28,857
  • 6
  • 80
  • 93
Saran
  • 179
  • 12

2 Answers2

1
sets1=[[['a', 'c'], ['a', 'e']], [ ['b', 'c'] , ['b', 'e']] ,[['a','z']] ]

a=[] 

for i in xrange(len(sets1)):
    b=[]
    for j in xrange(len(sets1[i])):
        for k in xrange(len(sets1[i][j])):
            if(sets1[i][j][k] not in b ):
                b.append(sets1[i][j][k])
    a.append(b)
print a
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
Saran
  • 179
  • 12
0
  1. Flatten your list of lists of 2d lists using @cdleary's solution: https://stackoverflow.com/a/406199/42346
  2. "Chunk" the resultant iterator object using @NedBatchelder's solution: https://stackoverflow.com/a/312464/42346

"Chunk" function:

def chunks(l, n):
    """ Yield successive n-sized chunks from l. """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

Example code:

import itertools as it
l = list(it.chain(*it.chain(*sets)))
print(list(chunks(l,3)))
# -> [['a', 'c', 'a'], ['e', 'b', 'c'], ['b', 'e', 'a'], ['z']]
Community
  • 1
  • 1
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • I don't think this is what the OP wanted....but your answer is the accepted answer. Very conflicted :P – miradulo Apr 20 '15 at 18:08
  • 1
    Yeah, I'm not sure why this is accepted. I missed a huge part of the question. Either your solution and San Sarai's solution should be accepted. – mechanical_meat Apr 20 '15 at 18:10