在列表理解中使用就地列表方法的替代方法?

2024-04-18 01:40:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道就地列表方法返回None,而不是变异的列表。据我所知,这使得不可能使用这些方法作为列表理解的内部逻辑的一部分。你知道吗

创建一个成员来自其他列表的变体的列表理解的最python的方法是什么?换句话说:除了这条(不起作用的)线路,最好的替代方案是什么:

new_list = [old_list.insert(0, "X") for old_list in list_of_old_lists]

结果是一个None的列表,因为list.insert()返回None。你知道吗

在一行优雅的代码中,如果没有大量的切片和串联,难道就不可能做到这一点吗?你知道吗

为了说明我的问题,上面的例子并不重要,但实际上我希望在更复杂的情况下这样做,而不是在多个嵌套的for循环中这样做。你知道吗

下面是我尝试做的一个简化示例:

word = 'abcdefg'

variations_list = []
characters_to_insert = ['X', 'Y', 'Z']

for character in characters_to_insert:
    for position in range(len(word) + 1):
        w = list(word)
        w.insert(position, character)
        this_variation = ''.join(w)
        variations_list.append(this_variation)

for v in variations_list:
    print(v)

使用嵌套的for循环可以很好地工作,就像这样(我的实际应用程序比这个示例要复杂得多)。你知道吗

但是我不能使用列表理解做同样的事情,因为'insert'方法返回None

variations_list_comprehension = [list(word).insert(position, character) for position in range(len(word) +1) for character in ['X', 'Y', 'Z']]

for v in variations_list_comprehension:
    print(v)

结果是None值的列表,因为原位突变返回“None”。你知道吗


Tags: to方法innone示例列表forposition
3条回答

不是每件事都应该或需要通过理解来解决。for-循环并不坏-有时它们比理解更好,特别是因为人们倾向于避免在一行中自动执行过多的操作。你知道吗

但是如果你真的想要一个列表理解解决方案,我会使用一个helper函数来包装就地操作:

def insert(it, index, value):
    lst = list(it)
    lst.insert(index, value)
    return lst

[''.join(insert(word, position, character)) for position in range(len(word) +1) for character in ['X', 'Y', 'Z']]

然而,要真正等同于你的循环解决方案,你需要在理解中交换循环:

[''.join(insert(word, position, character)) for character in ['X', 'Y', 'Z'] for position in range(len(word) +1)]

这里的优点是,包装in-place方法可以应用于很多情况,它不仅仅适用于这种情况(您可以这样包装任何in-place函数)。它很冗长,但可读性和可重用性都很强!你知道吗


我个人会使用带循环的生成器函数,因为您可以使用它创建列表,但也可以按需创建项目(不需要列表):

def insert_characters_everywhere(word, chars):
    for character in characters_to_insert:
        for position in range(len(word) + 1):
            lst = list(word)
            lst.insert(position, character)
            yield ''.join(lst)

list(insert_characters_everywhere('abcdefg', ['X', 'Y', 'Z']))

如果您不关心更改其他列表的结果,则不需要使用临时列表:

variations_list = [word[:i] + char + word[i:] 
                   for char in characters_to_insert 
                   for i in range(len(word) + 1)]

['Xabcdefg', 'aXbcdefg', 'abXcdefg', 'abcXdefg', 'abcdXefg', 'abcdeXfg', 'abcdefXg', 'abcdefgX', 
 'Yabcdefg', 'aYbcdefg', 'abYcdefg', 'abcYdefg', 'abcdYefg', 'abcdeYfg', 'abcdefYg', 'abcdefgY', 
 'Zabcdefg', 'aZbcdefg', 'abZcdefg', 'abcZdefg', 'abcdZefg', 'abcdeZfg', 'abcdefZg', 'abcdefgZ']

我仍然认为这充其量只是一种边缘理解:作为for循环更容易理解。你知道吗

我认为重要的是要了解你在这里到底想要达到什么目的。你知道吗

new_list = [old_list.insert(0, "X") for old_list in list_of_old_lists]

案例1:在上面的代码中,您是否试图创建一个新的new_list包含旧的(!!!)列表是否更新为包含'X'字符作为第一个元素?如果是这样,那么list_of_old_lists将是一个“新列表”列表。你知道吗

例如

list_of_old_lists = [['A'], ['B']]
new_list = []
for old_list in list_of_old_lists:
    old_list.insert(0, 'X')
    new_list.append(old_list)
print(list_of_old_lists)
print(new_list)
print(list_of_old_lists == new_list)

将打印:

[['X', 'A'], ['X', 'B']]
[['X', 'A'], ['X', 'B']]
True

也就是说,new_list是包含“更新”列表的list_of_old_lists的浅拷贝。如果这是您想要的,那么您可以使用列表理解执行以下操作:

[old_list.insert(0, "X") for old_list in list_of_old_lists]
new_list = list_of_old_lists[:]

而不是上面示例中的for-循环。你知道吗

案例2:或者,您是否试图创建一个包含更新列表的新列表,同时list_of_old_lists保留原始列表?在这种情况下,您可以通过以下方式使用列表理解:

new_list = [['X'] + old_list for old_list in list_of_old_lists]

然后:

In [14]:     list_of_old_lists = [['A'], ['B']]
    ...:     new_list = [['X'] + old_list for old_list in list_of_old_lists]
    ...:     print(list_of_old_lists)
    ...:     print(new_list)
    ...:     print(new_list == list_of_old_lists)
    ...: 
[['A'], ['B']]
[['X', 'A'], ['X', 'B']]
False

相关问题 更多 >