2

我编写了这个函数来过滤列表以在看到给定项目后显示所有项目。有点类似于内置的字符串方法str.rpartition(sep)。我觉得有一种更紧凑的方法可以做到这一点,也许使用列表理解。有任何想法吗?

def ignore_until(the_list, match):
    # Ignore all items in the_list prior to match
    found = False
    for index, item in enumerate(the_list):
        if item == match:
            found = True
            break
    if found:
        return the_list[index:]
    else:
        return []

my_list = ['red','orange','yellow','green']
assert ignore_until(my_list, 'yellow') == ['yellow','green']
assert ignore_until(my_list, 'blue') == []

编辑:

在看到上述问题的答案后,我意识到 6 个答案中有 5 个侧重于index()列表数据类型的内置方法。实际上,我需要使用正则表达式,并且没有意识到从我的问题中省略它会影响人们的答案。这是正则表达式代码:

import re
def ignore_until(the_list, pattern):
    # Ignore all items in the_list prior to the item containing pattern.
    found = False
    for index, item in enumerate(the_list):
        if re.search(string=item, pattern=pattern):
            found = True
            break
    if found:
        return the_list[index:]
    else:
        return []

my_list = ['red','orange','yellow','green']
assert ignore_until(my_list, 'yellow') == ['yellow','green']
assert ignore_until(my_list, 'blue') == []
4

6 回答 6

5

它并没有更紧凑,但是怎么样:

def ignore_until(the_list, match):
    try:
        return the_list[the_list.index(match):]
    except ValueError:
        return []

my_list = ['red','orange','yellow','green']

print ignore_until(my_list, 'yellow') # => ['yellow','green']
print ignore_until(my_list, 'blue') # => []
于 2012-05-12T16:19:41.910 回答
3

为什么不使用 pythonyourlist.index(match)查找索引然后应用列表切片。如果找不到匹配项,pythonyourlist.index会抛出错误,因此您需要注意这一点。

def ignore_until(yourlist, match):
    try:
        return yourlist[yourlist.index(match):]
    except ValueError:
        return []
于 2012-05-12T16:17:47.143 回答
2

这是一个重现 str.partition 所做的版本(即返回三个列表):

def partition(lst, item):
    if item in lst:
        n = lst.index(item)
        return lst[:n], [item], lst[n+1:]
    else:
        return lst, [], []

print partition(range(10), 7)

这是一个适用于任意迭代的版本,而不是必要的列表:

def partition(it, item):
    a = [[]]
    for x in it:
        if x == item and len(a) == 1:
            a.append([item])
            a.append([])
        else:
            a[-1].append(x)
    return a

print partition((x for x in range(10)), 7)

改良版:

def partition(it, item):
    a = []
    for x in it:
        if x == item:
            return a, [item], list(it)
        a.append(x)
    return a, [], []

print partition((x for x in range(10)), 7)
print partition((x for x in range(10)), 17)
于 2012-05-12T16:43:47.760 回答
1
def ignore_until(the_list, match):
    try:
        return my_list[the_list.index(match):]
    except ValueError:
        return []
于 2012-05-12T16:19:05.260 回答
1

试试这个:

def ignore_until(the_list, match):
    try:
        return [the_list[the_list.index(object):] for object in l if object == match][0]
    except IndexError:
        return []

有点难读,但很紧凑。

于 2012-05-12T16:21:23.367 回答
1

您是否考虑过使用 list.index() 方法?它返回指定项的第一个实例的索引(否则它会引发错误)

def ignore_until(the_list, match):
    if match in the_list:
        index = the_list.index(match)
        return the_list[index:]

    else:
        return []

来源:http ://docs.python.org/tutorial/datastructures.html

于 2012-05-12T16:22:23.070 回答