查找列表中较小列表的确切位置(python)

发布于 2024-12-12 01:59:49 字数 1355 浏览 0 评论 0原文

所以我有一个像这样的列表:

list=[10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 10.0, 10.0, 10.0, 10.2, 10.0, 9.9, 9.9, 9.9, 9.9, 10.0, 10.2, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.2, 10.5, 10.9, 10.5, 10.3, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.4, 10.7, 10.3, 10.2, 10.1, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 9.9, 9.9, 10.1, 9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.7, 9.8, 9.8, 9.7, 9.7, 9.7, 9.7, 9.7, 9.7, 9.6, 9.7]

然后我还有一个看起来像这样的子列表:

sublist=[9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8]

现在我需要对这个较小的列表做的是,我必须找到它在主列表中的位置。 所以在这种情况下,结果应该是这样的:index=119(我可能会偏离+/-1)

我一整天都在尝试这样做......并且在网络上没有找到任何东西...... 我有一些想法:

1)我找到列表中子列表的第一项......在本例中为 4,所以我检查下一个也是正确的数字,然后检查下一个错误的数字,它将发送它以在剩余列表[4:]中找到另一个 9.9 并再次执行相同的循环...直到找到完全匹配

2) 然后另一个想法是以某种方式使用字符串 str(list)[1:-1].find(str(sublist)[1:-1]),在这种情况下将给出 687 的答案...

这些想法的问题在于它们似乎很长并且草率而且我没能让这些想法发挥作用......

So i Have a list which is something like this:

list=[10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 10.0, 10.0, 10.0, 10.2, 10.0, 9.9, 9.9, 9.9, 9.9, 10.0, 10.2, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.2, 10.5, 10.9, 10.5, 10.3, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.4, 10.7, 10.3, 10.2, 10.1, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 9.9, 9.9, 10.1, 9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.7, 9.8, 9.8, 9.7, 9.7, 9.7, 9.7, 9.7, 9.7, 9.6, 9.7]

And then i also has a sublist which looks something like this:

sublist=[9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8]

now what I need to do with this smaller list, is that I have to find where this is located in the main list.
So in this case the result should be something like this: index=119 (I might be off with +/-1)

I've been trying to do this all day... And didnt find anything on the web...
I have a few ideas:

1) I find the first item of the sublist on the list....which in this case would be 4, so i check the next number which is also correct then the next which will be wrong and it would send it to find another 9.9 in the remaining list[4:] and do the same loop again...until an exact match is found

2) Then another idea is to somehow use strings
str(list)[1:-1].find(str(sublist)[1:-1]), which in this case would give the answer of 687...

Problem with these ideas is that they seem to be long and sloppy and also I haven't been able to make these ideas work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

巷雨优美回忆 2024-12-19 01:59:49

怎么样:

l = [10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 10.0, 10.0, 10.0, 10.2, 10.0, 9.9, 9.9, 9.9, 9.9, 10.0, 10.2, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.2, 10.5, 10.9, 10.5, 10.3, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.4, 10.7, 10.3, 10.2, 10.1, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 9.9, 9.9, 10.1, 9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.7, 9.8, 9.8, 9.7, 9.7, 9.7, 9.7, 9.7, 9.7, 9.6, 9.7]
subl = [9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8]
for i in xrange(len(l)-len(subl)):
  if l[i:i+len(subl)] == subl:
    print 'found at pos', i
    break
else:
  print 'not found'

这会打印 found at pos 118

PS 我已经重命名了变量,以便 list 不会隐藏内置函数。

How about:

l = [10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 10.0, 10.0, 10.0, 10.2, 10.0, 9.9, 9.9, 9.9, 9.9, 10.0, 10.2, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.2, 10.5, 10.9, 10.5, 10.3, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.4, 10.7, 10.3, 10.2, 10.1, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 9.9, 9.9, 10.1, 9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.7, 9.8, 9.8, 9.7, 9.7, 9.7, 9.7, 9.7, 9.7, 9.6, 9.7]
subl = [9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8]
for i in xrange(len(l)-len(subl)):
  if l[i:i+len(subl)] == subl:
    print 'found at pos', i
    break
else:
  print 'not found'

This prints found at pos 118.

P.S. I've renamed the variables so that list doesn't shadow the built-in function.

三月梨花 2024-12-19 01:59:49
ind = l.index(subl[0])
for i in xrange(l.count(subl[0])-1):
    if l[ind:ind+len(subl)] == subl:
        print (ind)
        break;
    ind = l.index(subl[0],ind+1)

这是一种有效的方法,只有当它至少知道 subl 的第一个值存在时才会进行比较。

ind = l.index(subl[0])
for i in xrange(l.count(subl[0])-1):
    if l[ind:ind+len(subl)] == subl:
        print (ind)
        break;
    ind = l.index(subl[0],ind+1)

This is an efficient way which will only compare when it knows at least the first value of subl is present.

可爱咩 2024-12-19 01:59:49

您的第二个想法可能会产生误报:如果子列表是单个值(例如 1),并且完整列表只有值 11,那么它将找到匹配项。如果您在字符串中添加了前导和尾随分隔符,则可以避免这种情况。

你的第一个想法已经是最佳解决方案的一半了;有一个算法(我现在忘记了它的名字)用于确定可以“重用”多少子字符串,这样您就不必在整个字符串中回溯。例如,假设您当前的候选人未通过,因为您的预期分数为 9.8,而您的分数却为 9.9;您不需要重新访问该元素,因为它与子字符串的第一个元素匹配。这些可以预先计算,因此您最终只需一次遍历完整列表即可。

Your second idea can give a false positive: if the sublist were a single value, say 1, and the full list had only the value 11, it would find a match. If you added leading and trailing separators into your string, this could be avoided.

Your first idea is halfway to the optimal solution; there is an algorithm (whose name escapes me at the moment) for determining how much of the substring you can "reuse", so that you never have to backtrack in the full string. For example, suppose your current candidate failed because you found a 9.9 where you expected a 9.8; you don't need to revisit that element, because it matches the first element of the substring. Those can be pre-computed, so you end up just walking down the full list in a single pass.

谜泪 2024-12-19 01:59:49
idx = next(i for i in range(len(lst)-len(sublst)) if lst[i:i+len(sublist)] == sublst)
# 118
idx = next(i for i in range(len(lst)-len(sublst)) if lst[i:i+len(sublist)] == sublst)
# 118
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文