-1

I currently have a list of lists like

L = [['E2', 'C1', 'A1', 'B1', 'C2'], ['C1', 'D1', 'A1'], ['C1', 'C2']]

My aim is to compare L[i] against L[i+1] to make groups. For example, L[2] is a subset of L[0] so I would eliminate it. My two different list will be L[0] and L[1].

For this, if I can somehow explode the list of list into different list, it will be easily achievable by iterating cmp(L[i], L[i+1]). By different list I mean, I will store each element as separate variable. => L_1 = L[0], L_2 = L[1] and L_3 = L[2].

zip only seems to do a kind of map between two lists. Can anyone suggest a function available?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Arun
  • 649
  • 8
  • 24
  • 1
    If you somehow stored the sublists as separate variables, you would effectively make it impossible to do what you want (iterate over them) – maillard Jul 29 '16 at 12:55
  • Possible duplicate of [Iterate through pairs of items in a Python list](http://stackoverflow.com/questions/5764782/iterate-through-pairs-of-items-in-a-python-list) – Two-Bit Alchemist Jul 29 '16 at 12:55
  • The lists **are** separate already. Accessing them through a local variable instead of an index makes no difference to what you're trying to do, except perhaps making it harder as the previous comment suggests. – Two-Bit Alchemist Jul 29 '16 at 12:56
  • Possible duplicate of [Iterate a list as pair (current, next) in Python](http://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python) – Mazdak Jul 29 '16 at 12:58

1 Answers1

0

You could map your list of list to create a list of sets. if you then iterate over that list, it would be possible to check if they are subsets of each other

LSets = map(set, L)

filteredL = filter(lambda s: not any(s.issubset(s2) for s2 in LSets if s != s2), LSets)
maillard
  • 680
  • 1
  • 14
  • 27
  • Thanks. This creates only set of all elements in listoflists. But, I intend to pickup or retain only L[0] & L[1] and not L[3] as it is already present in L[0]. – Arun Aug 01 '16 at 09:15
  • @Arun that's what the filter in the second code line is for. It filters out all sets that are a subset aof any of the other sets – maillard Aug 01 '16 at 12:42
  • Thanks. I agree with you. but If you take a look at the list, all the elements of L[2] is present in L[0] but not all elements of L[1] is present in L[0]. Hence my final output will be L[0] & L[1]. But here the filter creates only L[0] !. – Arun Aug 02 '16 at 11:52
  • @Arun When I paste the code from this answer into my python console (2.7.11) filteredL ends up with two sets; L[0] and L[1]. L[1] is not a subset of any of the other, so it is not removed. – maillard Aug 02 '16 at 17:08
  • Hi. I too have the same version in mac. But this is the output of filteredL, I get: [set(['A1', 'C2', 'C1', 'B1', 'E2'])] which is L[0] – Arun Aug 03 '16 at 11:07