集/线交点解

发布于 2024-09-03 16:29:45 字数 255 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铃予 2024-09-10 16:29:45

您可以采用两个列表中坐标的集合论交集:

intersecting_points = set(enumerate(list1)).intersection(set(enumerate(list2)))

...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:

intersecting_points = set(enumerate(list1)).intersection(set(enumerate(list2)))

...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.

薄荷→糖丶微凉 2024-09-10 16:29:45
from itertools import izip
def find_intersection(lineA, lineB):
  for pos, (A0, B0, A1, B1) in enumerate(izip(lineA, lineB, lineA[1:], lineB[1:])):
    #check integer intersections
    if A0 == B0: #check required if the intersection is at position 0
      return pos
    if A1 == B1: #check required if the intersection is at last position
      return pos + 1
    #check for intersection between points
    if (A0 > B0 and A1 < B1) or
       (A0 < B0 and A1 > B1):
      #intersection between pos and pos+1!
      return pos + solve_linear_equation(A0,A1,B0,B1)
  #no intersection
  return None

...其中 solve_linear_equation 找到段 (0,A0)→(1,A1)(0,B0)→(1,B1) 之间的交集

from itertools import izip
def find_intersection(lineA, lineB):
  for pos, (A0, B0, A1, B1) in enumerate(izip(lineA, lineB, lineA[1:], lineB[1:])):
    #check integer intersections
    if A0 == B0: #check required if the intersection is at position 0
      return pos
    if A1 == B1: #check required if the intersection is at last position
      return pos + 1
    #check for intersection between points
    if (A0 > B0 and A1 < B1) or
       (A0 < B0 and A1 > B1):
      #intersection between pos and pos+1!
      return pos + solve_linear_equation(A0,A1,B0,B1)
  #no intersection
  return None

...where solve_linear_equation finds the intersection between segments (0,A0)→(1,A1) and (0,B0)→(1,B1).

撞了怀 2024-09-10 16:29:45

我假设你的列表中的一个维度是假设的,例如 [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.

南城旧梦 2024-09-10 16:29:45
import itertools

def intersect_at_same_index(seq1, seq2):
    return (
        idx
        for idx, (item1, item2)
        in enumerate(itertools.izip(seq1, seq2))
        if item1 == item2).next()

这将返回两个序列具有相同项目的索引,如果所有项目对都不同,则引发 StopIteration。如果您不喜欢这种行为,请将 return 语句括在 try 语句中,并在 except StopIteration 子句中返回您最喜欢的失败指示符(例如 -1、None...)

import itertools

def intersect_at_same_index(seq1, seq2):
    return (
        idx
        for idx, (item1, item2)
        in enumerate(itertools.izip(seq1, seq2))
        if item1 == item2).next()

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 the except StopIteration clause return your favourite failure indicator (e.g. -1, None…)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文