i'm lost in list manipulation

GrelEns grelens at NOSPAMyahoo.NOTNEEDEDfr
Thu Mar 4 03:29:04 EST 2004


"GrelEns" <grelens at NOSPAMyahoo.NOTNEEDEDfr> a écrit dans le message de news:
4046e6d6$0$4677$626a14ce at news.free.fr...
>
> 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

i'm not happy with this code, must be an other shorter alternative, have you
any siggestions on how to build these sets  [[1], [2], [3], [1, 2], [1, 3],
[2, 3], [1, 2, 3]] from [1,2,3] ?

best





More information about the Python-list mailing list