Building Time Based Bins

alessandro -oggei- ogier alessandro.ogier at unimib.it
Sun Mar 20 23:04:16 CET 2005


MCD wrote:
> This goes on throughout a 12hr. period. I'd like to be able to place
> the low and high values of the additional fields in a single line
> divided into 5min intervals. So it would look something like this >>
> 
> 1235 22 88
> 1240 31 94

what about a sane list comprehension madness ? <g>

lines = """\
1231 23 56
1232 25 79
1234 26 88
1235 22 34
1237 31 85
1239 35 94
"""

input = lines.split('\n') # this is your input

div = lambda x: (x-1)/5

l = dict([
        (div(x), []) for x,y,z in [
            tuple([int(x) for x in x.split()]) for x in input if x
        ]
    ])

[
    l[x[0]].append(x[1]) for x in
    [
        [div(x), (x,y,z)] for x,y,z in
        [
            tuple([int(x) for x in x.split()]) for x in input if x
        ]
    ]
]

print [
        [max([x[0] for x in l[j]]),
         min([x[1] for x in l[j]]),
         max([x[2] for x in l[j]])
        ] for j in dict([
            (div(x), []) for x,y,z in [
                tuple([int(x) for x in x.split()]) for x in input
                if x
            ]
          ]).keys()
    ]


i think it's a bit memory hungry, though

cya,
-- 
Alessandro "oggei" Ogier <alessandro.ogier at unimib.it>
gpg --keyserver pgp.mit.edu --recv-keys EEBB4D0D





More information about the Python-list mailing list