list - Can I count on order being preserved in a Python tuple? -
i've got list of datetimes want construct time segments. in other words, turn [t0, t1, ... tn]
[(t0,t1),(t1,t2),...,(tn-1, tn)]
. i've done way:
# start sorting list of datetimes mdtimes.sort() # construct tuples represent possible start , end dates # left edges dtg0 = [x x in mdtimes] dtg0.pop() # right edges dtg1 = [x x in mdtimes] dtg1.reverse() dtg1.pop() dtg1.sort() dtsegs = zip(dtg0,dtg1)
questions...
- can count on tn-1 < tn (tn-1,tn) after i've created them way? (is ordering preserved?)
- is practice copy original
mdtimes
list list comprehensions? if not how should done? the purpose constructing these tuples iterate on them , segment data set
tn-1
,tn
. reasonable approach? i.e.datasegment = [x x in bigdata if ( (x['datetime'] > tleft) , (x['datetime'] < tright))]
thanks
tuple order insert values tuple. they're not going sorted think you're asking.
zip
again, retain order inserted values in.it's acceptable method, have 2 alternate suggestions: use copy module, or use
dtg1 = mdtimes[:]
.sounds reasonable.
Comments
Post a Comment