How should I handle inclusive ranges in Python? -
How should I handle inclusive ranges in Python? -
i working in domain in ranges conventionally described inclusively. have human-readable descriptions such from b
, represent ranges include both end points - e.g. from 2 4
means 2, 3, 4
.
what best way work these ranges in python code? next code works generate inclusive ranges of integers, need perform inclusive piece operations:
def inclusive_range(start, stop, step): homecoming range(start, (stop + 1) if step >= 0 else (stop - 1), step)
the finish solution see explicitly utilize + 1
(or - 1
) every time utilize range
or piece notation (e.g. range(a, b + 1)
, l[a:b+1]
, range(b, - 1, -1)
). repetition best way work inclusive ranges?
edit: l3viathan answering. writing inclusive_slice
function complement inclusive_range
option, although write follows:
def inclusive_slice(start, stop, step): ... homecoming slice(start, (stop + 1) if step >= 0 else (stop - 1), step)
...
here represents code handle negative indices, not straightforward when used slices - note, example, l3viathan's function gives wrong results if slice_to == -1
.
however, seems inclusive_slice
function awkward utilize - l[inclusive_slice(a, b)]
improve l[a:b+1]
?
is there improve way handle inclusive ranges?
edit 2: give thanks new answers. agree francis , corley changing meaning of piece operations, either globally or classes, lead important confusion. hence leaning towards writing inclusive_slice
function.
to reply own question previous edit, have come conclusion using such function (e.g. l[inclusive_slice(a, b)]
) improve manually adding/subtracting 1 (e.g. l[a:b+1]
), since allow border cases (such b == -1
, b == none
) handled in single place. can cut down awkwardness in using function?
edit 3: have been thinking how improve usage syntax, looks l[inclusive_slice(1, 5, 2)]
. in particular, if creation of inclusive piece resembled standard piece syntax. in order allow this, instead of inclusive_slice(start, stop, step)
, there function inclusive
takes piece parameter. ideal usage syntax inclusive
line 1
:
l[inclusive(1:5:2)] # 1 l[inclusive(slice(1, 5, 2))] # 2 l[inclusive(s_[1:5:2])] # 3 l[inclusive[1:5:2]] # 4 l[1:inclusive(5):2] # 5
unfortunately not permitted python, allows utilize of :
syntax within []
. inclusive
hence have called using either syntax 2
or 3
(where s_
acts the version provided numpy).
other possibilities create inclusive
object __getitem__
, permitting syntax 4
, or apply inclusive
stop
parameter of slice, in syntax 5
. unfortunately not believe latter can made work since inclusive
requires knowledge of step
value.
of workable syntaxes (the original l[inclusive_slice(1, 5, 2)]
, plus 2
, 3
, 4
), best use? or there another, improve option?
final edit: give thanks replies , comments, has been interesting. have been fan of python's "one way it" philosophy, issue has been caused conflict between python's "one way" , "one way" proscribed problem domain. have gained appreciation timtowtdi in language design.
for giving first , highest-voted answer, award bounty l3viathan.
write additional function inclusive slice, , utilize instead of slicing. while possible e.g. subclass list , implement __getitem__
reacting piece object, advise against it, since code behave contrary expectation — , you, too, in year.
inclusive_slice
this:
def inclusive_slice(mylist, slice_from=none, slice_to=none, step=1): if slice_to not none: slice_to += 1 if step > 0 else -1 if slice_to == 0: slice_to = none homecoming mylist[slice_from:slice_to:step]
what personally, utilize "complete" solution mentioned (range(a, b + 1)
, l[a:b+1]
) , comment well.
python range slice
Comments
Post a Comment