集/线交点解
我在 python 中有两个列表,我想知道它们是否在同一索引处相交。有数学方法可以解决这个问题吗?
例如,如果我有 [9,8,7,6,5] 和 [3,4,5,6,7] 我想要一个简单而有效的公式/算法来发现它们在索引 3 处相交。我知道我可以进行搜索,只是想知道是否有更好的方法。
我知道有一个公式可以通过将 y = mx + b 形式的两条线相减来求解它们,但我的“线”并不是真正的线,因为它仅限于列表中的项目并且可能有曲线。
任何帮助表示赞赏。
I have two lists in python and I want to know if they intersect at the same index. Is there a mathematical way of solving this?
For example if I have [9,8,7,6,5] and [3,4,5,6,7] I'd like a simple and efficient formula/algorithm that finds that at index 3 they intersect. I know I could do a search just wondering if there is a better way.
I know there is a formula to solve two lines in y = mx + b form by subtracting them from each other but my "line" isn't truly a line because its limited to the items in the list and it may have curves.
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以采用两个列表中坐标的集合论交集:
...enumerate 为您提供索引和值的元组的可迭代 - 换句话说, (0,9),(1,8),(2,7 ),ETC。
http://docs.python.org/library/stdtypes。 html#set-types-set-frozenset
...有意义吗?当然,这不会真正给你几何交集 - 例如,[1,2] 与 [2,1] 在 [x=0.5,y=1.5] 处相交 - 如果这就是你想要的,那么你必须解决每个区间的线性方程。
You could take the set-theoretic intersection of the coordinates in both lists:
...enumerate gives you an iterable of tuples of indexes and values - in other words, (0,9),(1,8),(2,7),etc.
http://docs.python.org/library/stdtypes.html#set-types-set-frozenset
...make sense? Of course, that won't truly give you geometric intersection - for example, [1,2] intersects with [2,1] at [x=0.5,y=1.5] - if that's what you want, then you have to solve the linear equations at each interval.
...其中
solve_linear_equation
找到段(0,A0)→(1,A1)
和(0,B0)→(1,B1) 之间的交集
。...where
solve_linear_equation
finds the intersection between segments(0,A0)→(1,A1)
and(0,B0)→(1,B1)
.我假设你的列表中的一个维度是假设的,例如 [9,8,7,6,5] 的高度为 x1,x2,x3,x4,x5,对吧?在这种情况下,您的列表将如何表示像 y=0 这样的曲线?
无论如何,我认为计算通用或随机曲线的交集没有任何捷径,最好的解决方案是进行有效的搜索。
I assume one dimension in your list is assumed e.g. [9,8,7,6,5] are heights at x1,x2,x3,x4,x5 right? in that case how your list will represent curves like y=0 ?
In any case I don't think there can be any shortcut for calculating intersection of generic or random curves, best solution is to do a efficient search.
这将返回两个序列具有相同项目的索引,如果所有项目对都不同,则引发 StopIteration。如果您不喜欢这种行为,请将 return 语句括在 try 语句中,并在 except StopIteration 子句中返回您最喜欢的失败指示符(例如 -1、None...)
This will return the index where the two sequences have equal items, and raise a
StopIteration
if all item pairs are different. If you don't like this behaviour, enclose the return statement in a try statement, and at theexcept StopIteration
clause return your favourite failure indicator (e.g. -1, None…)