Pythonic way to condese my array

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

    #1

    Pythonic way to condese my array

    I have a list of dictionaries where each dictionary defines, among other
    things, a row and column number. So, my list might look like this:
    [{'row':1, 'column':1, otherdata}, {'row':1, 'column':2, 'otherdata}...]

    This data is passed to flash and used there to create a grid of objects that
    are placed based on the row and column values.

    For a given set of data I may have values column values in the range of 1, 9
    inclusive.

    What I would like to do is take my list of dictionaries (like the one listed
    above) and shift the column numbers down to fill in missing values...
    example (only the column key,value pair is show for simplification) :

    [{'column':1}, {'column':2}, {'column', 4}, {'column': 8}, {'column':2},
    {'column', 4}]

    If this were the dataset... I would want to change all 4s to 3 and all 8s to
    4. That way I end up with 1 ... 4 instead of 1, 2, 4, and 8.

    Is there some slick way to do this?

    Thanks


  • Robert Kern

    #2
    Re: Pythonic way to condese my array

    Dean Card wrote:
    I have a list of dictionaries where each dictionary defines, among other
    things, a row and column number. So, my list might look like this:
    [{'row':1, 'column':1, otherdata}, {'row':1, 'column':2, 'otherdata}...]
    >
    This data is passed to flash and used there to create a grid of objects that
    are placed based on the row and column values.
    >
    For a given set of data I may have values column values in the range of 1, 9
    inclusive.
    >
    What I would like to do is take my list of dictionaries (like the one listed
    above) and shift the column numbers down to fill in missing values...
    example (only the column key,value pair is show for simplification) :
    >
    [{'column':1}, {'column':2}, {'column', 4}, {'column': 8}, {'column':2},
    {'column', 4}]
    >
    If this were the dataset... I would want to change all 4s to 3 and all 8s to
    4. That way I end up with 1 ... 4 instead of 1, 2, 4, and 8.
    >
    Is there some slick way to do this?
    Untested, but intended for Python 2.4:

    list_o_dicts = [{'column': 1}, ...]
    columns = set(x['column'] for x in list_o_dicts)
    column_map = dict((column, i+1) for i, column in enumerate(sorte d(columns)))
    for d in list_o_dicts:
    d['column'] = column_map[d['column']]

    Of course, code that I wouldn't get fired for would have a slightly greater line
    count and fewer one-letter variable names.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    • James Stroud

      #3
      Re: Pythonic way to condese my array

      Dean Card wrote:
      I have a list of dictionaries where each dictionary defines, among other
      things, a row and column number. So, my list might look like this:
      [{'row':1, 'column':1, otherdata}, {'row':1, 'column':2, 'otherdata}...]
      >
      This data is passed to flash and used there to create a grid of objects that
      are placed based on the row and column values.
      >
      For a given set of data I may have values column values in the range of 1, 9
      inclusive.
      >
      What I would like to do is take my list of dictionaries (like the one listed
      above) and shift the column numbers down to fill in missing values...
      example (only the column key,value pair is show for simplification) :
      >
      [{'column':1}, {'column':2}, {'column', 4}, {'column': 8}, {'column':2},
      {'column', 4}]
      >
      If this were the dataset... I would want to change all 4s to 3 and all 8s to
      4. That way I end up with 1 ... 4 instead of 1, 2, 4, and 8.
      >
      Is there some slick way to do this?
      >
      Thanks
      >
      >
      Not sure how slick this is, but I would do it thus:

      dicts = [{'column':1}, {'column':2}, {'column': 4}, {'column': 8},
      {'column':4}]

      vals = list(set(d['column'] for d in dicts))
      vals.sort()
      amap = dict((b,a) for (a,b) in enumerate(vals) )

      for d in dicts:
      d['column'] = amap[d['column']] + 1


      The "1" in the last line comes from your requirement to start numbering
      at 1 instead of 0.

      --
      James Stroud
      UCLA-DOE Institute for Genomics and Proteomics
      Box 951570
      Los Angeles, CA 90095


      Comment

      • Wildemar Wildenburger

        #4
        Re: Pythonic way to condese my array

        Robert Kern wrote:
        Of course, code that I wouldn't get fired for would have a slightly greater line
        count and fewer one-letter variable names.
        Great :D!
        That one made me laugh!

        wildemar

        Comment

        Working...