我应该如何处理 Python 中的包含范围?

编程入门 行业动态 更新时间:2024-10-17 20:30:25
本文介绍了我应该如何处理 Python 中的包含范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一个领域中工作,在该领域中,范围通常是包含性描述的.我有人类可读的描述,例如 from A to B ,它们表示包括两个端点的范围 - 例如from 2 to 4 表示2, 3, 4.

I am working in a domain in which ranges are conventionally described inclusively. I have human-readable descriptions such as from A to B , which represent ranges that include both end points - e.g. from 2 to 4 means 2, 3, 4.

在 Python 代码中处理这些范围的最佳方法是什么?以下代码用于生成包含范围的整数,但我还需要执行包含切片操作:

What is the best way to work with these ranges in Python code? The following code works to generate inclusive ranges of integers, but I also need to perform inclusive slice operations:

def inclusive_range(start, stop, step): return range(start, (stop + 1) if step >= 0 else (stop - 1), step)

我看到的唯一完整的解决方案是每次使用 range 或切片符号(例如 range(A, B + 1), l[A:B+1], range(B, A - 1, -1)).这种重复真的是处理包含范围的最佳方式吗?

The only complete solution I see is to explicitly use + 1 (or - 1) every time I use range or slice notation (e.g. range(A, B + 1), l[A:B+1], range(B, A - 1, -1)). Is this repetition really the best way to work with inclusive ranges?

感谢 L3viathan 的回答.编写一个 inclusive_slice 函数来补充 inclusive_range 肯定是一种选择,尽管我可能会这样写:

Thanks to L3viathan for answering. Writing an inclusive_slice function to complement inclusive_range is certainly an option, although I would probably write it as follows:

def inclusive_slice(start, stop, step): ... return slice(start, (stop + 1) if step >= 0 else (stop - 1), step)

... 这里表示处理负索引的代码,当与切片一起使用时,这些代码并不简单 - 请注意,例如,如果 slice_to == -1,L3viathan 的函数会给出不正确的结果.

... here represents code to handle negative indices, which are not straightforward when used with slices - note, for example, that L3viathan's function gives incorrect results if slice_to == -1.

然而,inclusive_slice 函数似乎很难使用 - l[inclusive_slice(A, B)] 真的比 l[A:B+1]?

However, it seems that an inclusive_slice function would be awkward to use - is l[inclusive_slice(A, B)] really any better than l[A:B+1]?

有没有更好的方法来处理包含范围?

Is there any better way to handle inclusive ranges?

编辑 2:感谢您提供新的答案.我同意 Francis 和 Corley 的观点,即改变切片操作的含义,无论是全局的还是某些类的,都会导致严重的混淆.因此,我现在倾向于编写 inclusive_slice 函数.

Edit 2: Thank you for the new answers. I agree with Francis and Corley that changing the meaning of slice operations, either globally or for certain classes, would lead to significant confusion. I am therefore now leaning towards writing an inclusive_slice function.

为了回答我之前编辑中的问题,我得出的结论是使用这样的函数(例如 l[inclusive_slice(A, B)])比手动添加/减去 1(例如 l[A:B+1]),因为它会允许边缘情况(例如 B == -1 和 B == None) 在一个地方处理.可以减少使用功能的尴尬吗?

To answer my own question from the previous edit, I have come to the conclusion that using such a function (e.g. l[inclusive_slice(A, B)]) would be better than manually adding/subtracting 1 (e.g. l[A:B+1]), since it would allow edge cases (such as B == -1 and B == None) to be handled in a single place. Can we reduce the awkwardness in using the function?

编辑 3: 我一直在考虑如何改进使用语法,目前看起来像 l[inclusive_slice(1, 5, 2)].特别是,如果包含切片的创建类似于标准切片语法,那就太好了.为了实现这一点,可以有一个函数 inclusive 将切片作为参数,而不是 inclusive_slice(start, stop, step).inclusive 的理想用法语法是 line 1:

Edit 3: I have been thinking about how to improve the usage syntax, which currently looks like l[inclusive_slice(1, 5, 2)]. In particular, it would be good if the creation of an inclusive slice resembled standard slice syntax. In order to allow this, instead of inclusive_slice(start, stop, step), there could be a function inclusive that takes a slice as a parameter. The ideal usage syntax for inclusive would be 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

不幸的是,Python 不允许这样做,它只允许在 [] 中使用 : 语法.因此,inclusive 必须使用语法 2 或 3(其中 s_ 的作用类似于 numpy 提供的版本).

Unfortunately this is not permitted by Python, which only allows the use of : syntax within []. inclusive would therefore have to be called using either syntax 2 or 3 (where s_ acts like the version provided by numpy).

其他可能性是将 inclusive 变成带有 __getitem__ 的对象,允许语法 4,或者应用 inclusive> 仅用于切片的 stop 参数,如语法 5.不幸的是,我不相信后者可以工作,因为 inclusive 需要了解 step 值.

Other possibilities are to make inclusive into an object with __getitem__, permitting syntax 4, or to apply inclusive only to the stop parameter of the slice, as in syntax 5. Unfortunately I do not believe the latter can be made to work since inclusive requires knowledge of the step value.

可行的语法(原始l[inclusive_slice(1, 5, 2)],加上2、3和>4),哪个最好用?或者还有其他更好的选择吗?

Of the workable syntaxes (the original l[inclusive_slice(1, 5, 2)], plus 2, 3 and 4), which would be the best to use? Or is there another, better option?

最终感谢大家的回复和评论,这非常有趣.我一直是 Python 的一种方式来做"哲学的粉丝,但是这个问题是由 Python 的一种方式"和问题域所禁止的一种方式"之间的冲突引起的.我确实对 TIMTOWTDI 在语言设计方面获得了一些赞赏.

Final Thank you all for the replies and comments, this has been very interesting. I have always been a fan of Python's "one way to do it" philosophy, but this issue has been caused by a conflict between Python's "one way" and the "one way" proscribed by the problem domain. I have definitely gained some appreciation for TIMTOWTDI in language design.

由于给出了第一个和最高票数的答案,我将赏金奖励给了 L3viathan.

For giving the first and highest-voted answer, I award the bounty to L3viathan.

推荐答案

为包含切片编写一个额外的函数,并使用它代替切片.虽然有可能例如子类列表并实现对切片对象做出反应的 __getitem__ ,我建议不要这样做,因为您的代码将与除您之外的任何人的期望相反 - 并且可能在一年内对您也是如此.

Write an additional function for inclusive slice, and use that instead of slicing. While it would be possible to e.g. subclass list and implement a __getitem__ reacting to a slice object, I would advise against it, since your code will behave contrary to expectation for anyone but you — and probably to you, too, in a year.

inclusive_slice 可能如下所示:

def inclusive_slice(myList, slice_from=None, slice_to=None, step=1): if slice_to is not None: slice_to += 1 if step > 0 else -1 if slice_to == 0: slice_to = None return myList[slice_from:slice_to:step]

我个人会做的就是使用您提到的完整"解决方案 (range(A, B + 1), l[A:B+1]>) 和评论.

What I would do personally, is just use the "complete" solution you mentioned (range(A, B + 1), l[A:B+1]) and comment well.

更多推荐

我应该如何处理 Python 中的包含范围?

本文发布于:2023-10-18 11:55:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!