查找列表中子列表出现的(开始:结束)位置。 Python

发布于 2024-12-20 20:47:42 字数 644 浏览 4 评论 0原文

如果有一长串数字:

example=['130','90','150','123','133','120','160','45','67','55','34']

以及列表中的子列表,例如

sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

如何生成一个函数来获取这些子列表并给出它们在原始字符串中出现的位置? 得到结果:

results=[[0-2],[1-2],[5-8]]

我正在尝试一些类似的事情,

example=['130','90','150','123','133','120','160','45','67','55','34']

sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

for p in range(len(example)):
    for lists in sub_lists:
        if lists in example:
            print p

但那不起作用?

If one had a long list of numbers:

example=['130','90','150','123','133','120','160','45','67','55','34']

and sub lists within the list like

sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

how would you generate a function that takes these sublists and gives you the positions that they occurred in the original string?
to get the results:

results=[[0-2],[1-2],[5-8]]

I was trying something along the lines of

example=['130','90','150','123','133','120','160','45','67','55','34']

sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

for p in range(len(example)):
    for lists in sub_lists:
        if lists in example:
            print p

but that was not working?

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

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

发布评论

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

评论(2

寻梦旅人 2024-12-27 20:47:43

这应该可以处理几乎所有情况,包括多次出现的子列表:

example=['130','90','150','123','133','120','160','45','67','55','34']
sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

for i in range(len(example)):
    for li in sub_lists:
        length = len(li)
        if example[i:i+length] == li:
            print 'List %s has been matched at index [%d, %d]' % (li, i, i+length-1)

输出:

List ['130', '90', '150'] has been matched at index [0, 2]
List ['90', '150'] has been matched at index [1, 2]
List ['120', '160', '45', '67'] has been matched at index [5, 8]

This should handle almost any case, including a sublist being present more than once:

example=['130','90','150','123','133','120','160','45','67','55','34']
sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

for i in range(len(example)):
    for li in sub_lists:
        length = len(li)
        if example[i:i+length] == li:
            print 'List %s has been matched at index [%d, %d]' % (li, i, i+length-1)

Outputs:

List ['130', '90', '150'] has been matched at index [0, 2]
List ['90', '150'] has been matched at index [1, 2]
List ['120', '160', '45', '67'] has been matched at index [5, 8]
方圜几里 2024-12-27 20:47:43

这是可行的,但只是因为我依赖于子列表以其完整性存在的事实

example=['130','90','150','123','133','120','160','45','67','55','34']

sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

def f(example, sub_lists) :
    for l in sub_lists:
        yield [example.index(l[0]),example.index(l[-1])]

print [x for x in f(example,sub_lists)]

>>> [[0, 2], [1, 2], [5, 8]]

This works, but only because I depend on the facts that the sublists exists in their interity

example=['130','90','150','123','133','120','160','45','67','55','34']

sub_lists=[['130','90','150'],['90','150'],['120','160','45','67']]

def f(example, sub_lists) :
    for l in sub_lists:
        yield [example.index(l[0]),example.index(l[-1])]

print [x for x in f(example,sub_lists)]

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