SHARE
TWEET

Untitled

a guest Feb 28th, 2020 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import itertools
  2. import functools
  3. def powerlist(l):
  4.  
  5.     indexes = {}
  6.     for i, e in enumerate(l):
  7.         indexes[e] = i
  8.    
  9.     combos = []
  10.     for i in range(1, len(l) + 1):
  11.         combos = combos + list(list(tup) for tup in itertools.combinations(l, i))
  12.    
  13.     def compare(item1, item2):
  14.         if indexes[item1[-1]] < indexes[item2[-1]]:
  15.             return -1
  16.         elif indexes[item1[-1]] > indexes[item2[-1]]:
  17.             return 1
  18.         else:
  19.             if len(item1) < len(item2):
  20.                 return -1
  21.             elif len(item1) > len(item2):
  22.                 return 1
  23.             else:
  24.                 for i in range(len(item1) - 1, -1, -1):
  25.                     if indexes[item1[i]] < indexes[item2[i]]:
  26.                         return -1
  27.                     elif indexes[item1[i]] > indexes[item2[i]]:
  28.                         return 1
  29.                 return 0
  30.  
  31.     combos.sort(key=functools.cmp_to_key(compare))
  32.     combos = [[]] + combos
  33.     return combos
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top