i'm lost in list manipulation

GrelEns grelens at NOSPAMyahoo.NOTNEEDEDfr
Thu Mar 4 03:20:55 EST 2004


btw please give your opinion on this one :

def build(l, n):
 result = []
 if n == 1:
  for e in l:
   result.append([e])
 else:
  subSet = build(l, n - 1)
  for n_uples in subSet:
   iLast = l.index(n_uples[-1])
   if iLast < len(l) - 1:
    for e in l[iLast + 1:]:
     tmp = n_uples + [e]
     result.append(tmp)
 return result

def allSets(l):
 """from [1,2,3] will build [[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2,
3]]"""
 result = []
 for i in range(1, len(l)):
  result += build(l, i)
 result.append(l)
 return result

def do_it(l):
 r = range(1,len(l))
 result = []
 for x in allSets(r):
  tmp = []
  for i in range(len(l)):
   if i in x:
    tmp.append('-')
   tmp.append(l[i])
  result.append(tmp)
 return result

>>> do_it(l)
[['a', '-', 'b', 'c', 'd'], ['a', 'b', '-', 'c', 'd'], ['a', 'b', 'c', '-',
'd'], ['a', '-', 'b', '-', 'c', 'd'], ['a', '-', 'b', 'c', '-', 'd'], ['a',
'b', '-', 'c', '-', 'd'], ['a', '-', 'b', '-', 'c', '-', 'd']]

best





More information about the Python-list mailing list