Creating a new data structure while filtering its data origin.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    Creating a new data structure while filtering its data origin.

    Hi everyone.

    I'm trying to work with very simple data structures but I'm stuck in the very first steps. If someone has the luxury of a few minutes and can give an advice how to resolve this, I'll really appreciate it.

    1- I have a list of tuples like this:
    lista= [(162, 141, 3), (162, 141, 3), (162, 141, 3), (168, 141, 2), (168, 141, 2), (168, 141, 2), (201, 141, 1), (213, 141, 1), (203, 141, 1), (562, 142, 4), (562, 142, 4), (562, 142, 4), (568, 142, 2), (568, 142, 2), (568, 142, 2), (501, 142, 1), (513, 142, 1), (503, 142, 1)]
    and I want to end with a dict like this:
    {141: {1: [203, 213, 201], 2: [168, ], 3: [162, ]}, 142: {1: [503, 513, 501], 2: [568, ], 4: [562, ]}}
    the logic of the final output:
    a) the outer dict's key is a set() of the 2rd value of the input.
    b) the inner dict's key is a set() of the 3th value for tuples which 3rd value equals a).
    c) the inner list will be fill up with the 1st value of every tuple which 3rd value equals b) and its 2rd value equals a).

    So far, the only thing it seems I can achieve is the first part:
    outer_dict = dict([(x,dict()) for x in set(row[1] for row in lista)])
    >From then on, I'm starting to get tired after several successful failures (I tried with itertools, with straight loops ...) and I don't know which can be the easier way to get that final output.
    Thanks in advance.


  • Larry Bates

    #2
    Re: Creating a new data structure while filtering its data origin.

    mmara@fibertel. com.ar wrote:
    Hi everyone.
    >
    I'm trying to work with very simple data structures but I'm stuck in the very first steps. If someone has the luxury of a few minutes and can give an advice how to resolve this, I'll really appreciate it.
    >
    1- I have a list of tuples like this:
    lista= [(162, 141, 3), (162, 141, 3), (162, 141, 3), (168, 141, 2), (168, 141, 2), (168, 141, 2), (201, 141, 1), (213, 141, 1), (203, 141, 1), (562, 142, 4), (562, 142, 4), (562, 142, 4), (568, 142, 2), (568, 142, 2), (568, 142, 2), (501, 142, 1), (513, 142, 1), (503, 142, 1)]
    and I want to end with a dict like this:
    {141: {1: [203, 213, 201], 2: [168, ], 3: [162, ]}, 142: {1: [503, 513, 501], 2: [568, ], 4: [562, ]}}
    the logic of the final output:
    a) the outer dict's key is a set() of the 2rd value of the input.
    b) the inner dict's key is a set() of the 3th value for tuples which 3rd value equals a).
    c) the inner list will be fill up with the 1st value of every tuple which 3rd value equals b) and its 2rd value equals a).
    >
    So far, the only thing it seems I can achieve is the first part:
    outer_dict = dict([(x,dict()) for x in set(row[1] for row in lista)])
    >
    >>From then on, I'm starting to get tired after several successful failures (I tried with itertools, with straight loops ...) and I don't know which can be the easier way to get that final output.
    >
    Thanks in advance.
    >
    >
    d={}
    for a, b, c in lista:
    if d.has_key(b):
    if d[b].has_key(c):
    if a not in d[b][c]: d[b][c].append(a)

    else:
    d[b][c]=[a]
    else:
    d[b]={c:[a]}


    print d

    -Larry Bates

    Comment

    • Larry Bates

      #3
      Re: Creating a new data structure while filtering its data origin.

      mmara@fibertel. com.ar wrote:
      Hi everyone.
      >
      I'm trying to work with very simple data structures but I'm stuck in the very first steps. If someone has the luxury of a few minutes and can give an advice how to resolve this, I'll really appreciate it.
      >
      1- I have a list of tuples like this:
      lista= [(162, 141, 3), (162, 141, 3), (162, 141, 3), (168, 141, 2), (168, 141, 2), (168, 141, 2), (201, 141, 1), (213, 141, 1), (203, 141, 1), (562, 142, 4), (562, 142, 4), (562, 142, 4), (568, 142, 2), (568, 142, 2), (568, 142, 2), (501, 142, 1), (513, 142, 1), (503, 142, 1)]
      and I want to end with a dict like this:
      {141: {1: [203, 213, 201], 2: [168, ], 3: [162, ]}, 142: {1: [503, 513, 501], 2: [568, ], 4: [562, ]}}
      the logic of the final output:
      a) the outer dict's key is a set() of the 2rd value of the input.
      b) the inner dict's key is a set() of the 3th value for tuples which 3rd value equals a).
      c) the inner list will be fill up with the 1st value of every tuple which 3rd value equals b) and its 2rd value equals a).
      >
      So far, the only thing it seems I can achieve is the first part:
      outer_dict = dict([(x,dict()) for x in set(row[1] for row in lista)])
      >
      >>From then on, I'm starting to get tired after several successful failures (I tried with itertools, with straight loops ...) and I don't know which can be the easier way to get that final output.
      >
      Thanks in advance.
      >
      >
      d={}
      for a, b, c in lista:
      if d.has_key(b):
      if d[b].has_key(c):
      if a not in d[b][c]: d[b][c].append(a)

      else:
      d[b][c]=[a]
      else:
      d[b]={c:[a]}


      print d

      -Larry Bates

      Comment

      • FlipFish2007@yahoo.com

        #4
        Re: Creating a new data structure while filtering its data origin.

        On Mar 28, 4:44 pm, <m...@fibertel. com.arwrote:
        Hi everyone.
        >
        I'm trying to work with very simple data structures but I'm stuck in the very first steps. If someone has the luxury of a few minutes and can give an advice how to resolve this, I'll really appreciate it.
        >
        1- I have a list of tuples like this:
        lista= [(162, 141, 3), (162, 141, 3), (162, 141, 3), (168, 141, 2), (168, 141, 2), (168, 141, 2), (201, 141, 1), (213, 141, 1), (203, 141, 1), (562, 142, 4), (562, 142, 4), (562, 142, 4), (568, 142, 2), (568, 142, 2), (568, 142, 2), (501, 142, 1), (513, 142, 1), (503, 142, 1)]
        and I want to end with a dict like this:
        {141: {1: [203, 213, 201], 2: [168, ], 3: [162, ]}, 142: {1: [503, 513, 501], 2: [568, ], 4: [562, ]}}
        the logic of the final output:
        a) the outer dict's key is a set() of the 2rd value of the input.
        b) the inner dict's key is a set() of the 3th value for tuples which 3rd value equals a).
        c) the inner list will be fill up with the 1st value of every tuple which 3rd value equals b) and its 2rd value equals a).
        >
        So far, the only thing it seems I can achieve is the first part:
        outer_dict = dict([(x,dict()) for x in set(row[1] for row in lista)])
        >
        From then on, I'm starting to get tired after several successful failures (I tried with itertools, with straight loops ...) and I don't know which can be the easier way to get that final output.
        >
        Thanks in advance.
        dict([[b,dict([[k, dict([[x, None] for (x,y,z) in lista if y==b and
        z==k]).keys()] for (i,j,k) in lista if j==b])] for (a,b,c) in lista])

        Comment

        • attn.steven.kuo@gmail.com

          #5
          Re: Creating a new data structure while filtering its data origin.

          On Mar 28, 1:44 pm, <m...@fibertel. com.arwrote:
          Hi everyone.
          >
          I'm trying to work with very simple data structures but I'm stuck in the very first steps. If someone has the luxury of a few minutes and can give an advice how to resolve this, I'll really appreciate it.
          >
          1- I have a list of tuples like this:
          lista= [(162, 141, 3), (162, 141, 3), (162, 141, 3), (168, 141, 2), (168, 141, 2), (168, 141, 2), (201, 141, 1), (213, 141, 1), (203, 141, 1), (562, 142, 4), (562, 142, 4), (562, 142, 4), (568, 142, 2), (568, 142, 2), (568, 142, 2), (501, 142, 1), (513, 142, 1), (503, 142, 1)]
          and I want to end with a dict like this:
          {141: {1: [203, 213, 201], 2: [168, ], 3: [162, ]}, 142: {1: [503, 513, 501], 2: [568, ], 4: [562, ]}}

          (snipped)

          I think you can use, with python 2.5, defaultdict for
          this. I only have access to 2.4.4 here, so my facsimile
          is:

          class defaultd(dict):
          def __getitem__(sel f, k):
          if k not in self:
          self[k] = defaultd()
          return dict.__getitem_ _(self, k)

          lista = [(162, 141, 3), (162, 141, 3), (162, 141, 3), (168, 141, 2),
          (168, 141, 2), (168, 141, 2), (201, 141, 1), (213, 141, 1), (203,
          141,
          1), (562, 142, 4), (562, 142, 4), (562, 142, 4), (568, 142, 2), (568,
          142, 2), (568, 142, 2), (501, 142, 1), (513, 142, 1), (503, 142, 1)]

          dd = defaultd()

          for value, outerkey, innerkey in lista:
          li = dd[outerkey].setdefault(inn erkey, [])
          if value not in li:
          li.insert(0, value)
          # or, li.append(value ) if you want to reverse order

          print dd

          I happen to like the "autovivificati on" feature
          found in perl and was pleased to see defaultdict in
          python 2.5. OTOH, python programmers more experienced
          than I might see this and run away screaming.


          --
          Hope this helps,
          Steven

          Comment

          • Scott David Daniels

            #6
            Re: Creating a new data structure while filtering its data origin.

            mmara@fibertel. com.ar wrote:
            Hi everyone.
            >
            I'm trying to work with very simple data structures but I'm stuck in the very first steps. If someone has the luxury of a few minutes and can give an advice how to resolve this, I'll really appreciate it.
            >
            1- I have a list of tuples like this:
            lista= [(162, 141, 3), (162, 141, 3), (162, 141, 3), (168, 141, 2), (168, 141, 2), (168, 141, 2), (201, 141, 1), (213, 141, 1), (203, 141, 1), (562, 142, 4), (562, 142, 4), (562, 142, 4), (568, 142, 2), (568, 142, 2), (568, 142, 2), (501, 142, 1), (513, 142, 1), (503, 142, 1)]
            and I want to end with a dict like this:
            {141: {1: [203, 213, 201], 2: [168, ], 3: [162, ]}, 142: {1: [503, 513, 501], 2: [568, ], 4: [562, ]}}
            the logic of the final output:
            a) the outer dict's key is a set() of the 2rd value of the input.
            b) the inner dict's key is a set() of the 3th value for tuples which 3rd value equals a).
            c) the inner list will be fill up with the 1st value of every tuple which 3rd value equals b) and its 2rd value equals a).
            >
            So far, the only thing it seems I can achieve is the first part:
            outer_dict = dict([(x,dict()) for x in set(row[1] for row in lista)])
            >
            >>From then on, I'm starting to get tired after several successful failures (I tried with itertools, with straight loops ...) and I don't know which can be the easier way to get that final output.
            >
            Thanks in advance.
            >
            >
            lista, wanted = ...
            result = {}
            for a,b,c in lista:
            inner = result.setdefau lt(b, {}).setdefault( c, [])
            if a not in inner:
            inner.insert(0, a) # I had used append, but ...
            print result == wanted

            --Scott David Daniels
            scott.daniels@a cm.org

            Comment

            Working...