如果要列出 list ,请说:

lst = ['foo', 'bar', '!test', 'hello', 'world!', 'word']

使用!字符,我如何返回给定的列表:
lst = ['foo', 'bar', ['test', 'hello', 'world'], 'word']

我在为此找到解决方案时遇到了一些困难。这是我尝试过的一种方法:
def define(lst):
    for index, item in enumerate(lst):
        if item[0] == '!' and lst[index+2][-1] == '!':
            temp = lst[index:index+3]
            del lst[index+1:index+2]
            lst[index] = temp
    return lst

任何帮助将不胜感激。

最佳答案

假设没有像!这样以'!foo!'开头和结尾的元素。

首先,我们可以编写如下的辅助谓词

def is_starting_element(element):
    return element.startswith('!')


def is_ending_element(element):
    return element.endswith('!')

然后我们可以写generator-function(因为它们很棒)
def walk(elements):
    elements = iter(elements)  # making iterator from passed iterable
    for position, element in enumerate(elements):
        if is_starting_element(element):
            yield [element[1:], *walk(elements)]
        elif is_ending_element(element):
            yield element[:-1]
            return
        else:
            yield element

测试:
>>> lst = ['foo', 'bar', '!test', 'hello', 'world!', 'word']
>>> list(walk(lst))
['foo', 'bar', ['test', 'hello', 'world'], 'word']
>>> lst = ['foo', 'bar', '!test', '!hello', 'world!', 'word!']
>>> list(walk(lst))
['foo', 'bar', ['test', ['hello', 'world'], 'word']]
>>> lst = ['hello!', 'world!']
>>> list(walk(lst))
['hello']

从上一个示例中可以看出,如果关闭元素的数量大于打开元素的数量,则剩余的关闭元素将被忽略(这是因为我们是从generator生成的return编码)。因此,如果lst具有无效的签名(开始元素和结束元素之间的差不等于0),那么我们可能会有一些不可预测的行为。作为这种情况的一种解决方法,我们可以在处理之前验证给定的数据,如果数据无效,则引发错误。

我们可以这样写验证器
def validate_elements(elements):
    def get_sign(element):
        if is_starting_element(element):
            return 1
        elif is_ending_element(element):
            return -1
        else:
            return 0

    signature = sum(map(get_sign, elements))
    are_elements_valid = signature == 0
    if not are_elements_valid:
        error_message = 'Data is invalid: '
        if signature > 0:
            error_message += ('there are more opening elements '
                              'than closing ones.')
        else:
            error_message += ('there are more closing elements '
                              'than opening ones.')
        raise ValueError(error_message)

测验
>>> lst = ['!hello', 'world!']
>>> validate_elements(lst)  # no exception raised, data is valid
>>> lst = ['!hello', '!world']
>>> validate_elements(lst)
...
ValueError: Data is invalid: there are more opening elements than closing ones.
>>> lst = ['hello!', 'world!']
>>> validate_elements(lst)
...
ValueError: Data is invalid: there are more closing elements than opening ones.

最后,我们可以编写带有验证的函数,例如
def to_sublists(elements):
    validate_elements(elements)
    return list(walk(elements))

测验
>>> lst = ['foo', 'bar', '!test', 'hello', 'world!', 'word']
>>> to_sublists(lst)
['foo', 'bar', ['test', 'hello', 'world'], 'word']
>>> lst = ['foo', 'bar', '!test', '!hello', 'world!', 'word!']
>>> to_sublists(lst)
['foo', 'bar', ['test', ['hello', 'world'], 'word']]
>>> lst = ['hello!', 'world!']
>>> to_sublists(lst)
...
ValueError: Data is invalid: there are more closing elements than opening ones.

编辑

如果我们要处理以!之类的'!bar!'开头和结尾的元素,我们可以使用 walk 修改itertools.chain函数,例如
from itertools import chain


def walk(elements):
    elements = iter(elements)
    for position, element in enumerate(elements):
        if is_starting_element(element):
            yield list(walk(chain([element[1:]], elements)))
        elif is_ending_element(element):
            element = element[:-1]
            yield element
            return
        else:
            yield element

我们还需要通过修改get_sign函数来完成验证
def get_sign(element):
    if is_starting_element(element):
        if is_ending_element(element):
            return 0
        return 1
    if is_ending_element(element):
        return -1
    return 0

测验
>>> lst = ['foo', 'bar', '!test', '!baz!', 'hello', 'world!', 'word']
>>> to_sublists(lst)
['foo', 'bar', ['test', ['baz'], 'hello', 'world'], 'word']

10-06 01:01