i'm lost in list manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GrelEns

    i'm lost in list manipulation

    hello,

    having a list like ['a', 'b', 'c', 'd'] i would like to get

    [['a', '-', 'b', 'c', 'd'],
    ['a', 'b', '-', 'c', 'd'],
    ['a', 'b', 'c', '-', 'd'],
    ['a', '-', 'b', '-', 'c', 'd'],
    ['a', 'b', '-', 'c', '-', 'd'],
    ['a', '-', 'b', '-', 'c','-', 'd']]

    that is adding successively an item between at all the available place, and
    i just can't get my brain doing that :(

    thank a lot if you could help...


  • Scott David Daniels

    #2
    Re: i'm lost in list manipulation

    GrelEns wrote:
    [color=blue]
    > hello,
    >
    > having a list like ['a', 'b', 'c', 'd'] i would like to get
    >
    > [['a', '-', 'b', 'c', 'd'],
    > ['a', 'b', '-', 'c', 'd'],
    > ['a', 'b', 'c', '-', 'd'],
    > ['a', '-', 'b', '-', 'c', 'd'],
    > ['a', 'b', '-', 'c', '-', 'd'],
    > ['a', '-', 'b', '-', 'c','-', 'd']]
    >
    > that is adding successively an item between at all the available place, and
    > i just can't get my brain doing that :([/color]
    Maybe:
    Think of all possible places. How many are there? At each of these
    places you either put a dash or not. Sounds like binary to me.

    --
    -Scott David Daniels
    Scott.Daniels@A cm.Org

    Comment

    • wes weston

      #3
      Re: i'm lost in list manipulation



      GrelEns wrote:[color=blue]
      > hello,
      >
      > having a list like ['a', 'b', 'c', 'd'] i would like to get
      >
      > [['a', '-', 'b', 'c', 'd'],
      > ['a', 'b', '-', 'c', 'd'],
      > ['a', 'b', 'c', '-', 'd'],
      > ['a', '-', 'b', '-', 'c', 'd'],
      > ['a', 'b', '-', 'c', '-', 'd'],
      > ['a', '-', 'b', '-', 'c','-', 'd']]
      >
      > that is adding successively an item between at all the available place, and
      > i just can't get my brain doing that :(
      >
      > thank a lot if you could help...
      >
      >[/color]


      GrelEns,
      Scott has it. For a start string of 4 chars there are 3 gaps/spaces
      for the dash/not dash. So, there are 8 possible results. One of these
      has no dashes - you may want to throw this one out. The "gap" strings
      needs to be:
      "000"
      "001"
      "010"
      "011"
      "100"
      "101"
      "110"
      "111"
      where the 1's are your character; "-"
      Throw out the first one and insert into the gaps in the original string.
      wes

      Comment

      • Paul Watson

        #4
        Re: i'm lost in list manipulation


        "GrelEns" <grelens@NOSPAM yahoo.NOTNEEDED fr> wrote in message
        news:40460bb3$0 $28116$636a15ce @news.free.fr.. .[color=blue]
        > hello,
        >
        > having a list like ['a', 'b', 'c', 'd'] i would like to get
        >
        > [['a', '-', 'b', 'c', 'd'],
        > ['a', 'b', '-', 'c', 'd'],
        > ['a', 'b', 'c', '-', 'd'],
        > ['a', '-', 'b', '-', 'c', 'd'],
        > ['a', 'b', '-', 'c', '-', 'd'],
        > ['a', '-', 'b', '-', 'c','-', 'd']]
        >
        > that is adding successively an item between at all the available place,[/color]
        and[color=blue]
        > i just can't get my brain doing that :(
        >
        > thank a lot if you could help...[/color]

        Does this look like it would work?

        alist = ['a', 'b', 'c', 'd']
        nlist = range(len(alist ) - 1)
        finallist = []

        for x in range(2 ** (len(alist) - 1)):
        if x == 0: continue #remove this if you want one with no dashes
        rlist = [alist[0]]
        for y in nlist:
        if x & (1 << y): rlist.append('-')
        rlist.append(al ist[y + 1])
        finallist.appen d(rlist)
        del rlist

        print finallist


        $ ./listins.py
        [['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']]


        Comment

        Working...