简体   繁体   English

一种用于根据条件弹出列表列表的衬垫

[英]One liner for poping list of lists based on condition

I've got the following lists:我有以下列表:

leftoverbricks = [['purple1', 'y8', 'x0', 'y8', 'x1'], ['purple2', 'y6', 'y0', 'x8', 'y0'], ['purple3', 'z2', 'x8', 'z2', 'x0']]

and

startingbrick = ['purple3', 'z2', 1, 1]

I'd like to pop an element from leftoverbricks where startingbrick[0] matches first element of list of list from leftoverbricks, so leftoverbricks[][0]我想从 leftoverbricks 中弹出一个元素,其中startingbrick[0]与来自 leftoverbricks 的列表列表的第一个元素匹配,所以leftoverbricks[][0]

I've created a function that works:我创建了一个有效的函数:

def removebrick(tempbrick, templist):
    reducedlist = templist
    for tempelement in reducedlist:
        if tempelement[0] == tempbrick[0]:
            reducedlist.pop(reducedlist.index(tempelement))
    return reducedlist

and which gives the correct result of:并给出正确的结果:

reducedleftoverbricks = removebrick(startingbrick, leftoverbricks)

reducedleftoverbricks = [['purple1', 'y8', 'x0', 'y8', 'x1'], ['purple2', 'y6', 'y0', 'x8', 'y0']]

But it's not elegant.但这并不优雅。 I hope such thing can be done with one liner and by mutating original leftoverbricks list rather than creating a new list variable reducedleftoverbricks .我希望这样的事情可以用一个衬垫并通过改变原始的reducedleftoverbricks leftoverbricks I did a few attempts at one liner list comprehension but so far failed.我在一个班轮列表理解上做了几次尝试,但到目前为止都失败了。

Personally, I'd consider using a filter.就个人而言,我会考虑使用过滤器。 Note that the filter is going to be lazily evaluated, so you can evaluate the whole filter in the spot where you'd like the reduced list.请注意,过滤器将被延迟评估,因此您可以在您想要减少列表的地方评估整个过滤器。

for brick in filter(lambda n: n[0] != startingbrick[0], leftoverbricks):
   do_more_work(brick)

This would be a good use-case for filter() .这将是filter()的一个很好的用例。

eg,例如,

leftoverbricks = [['purple1', 'y8', 'x0', 'y8', 'x1'], ['purple2', 'y6', 'y0', 'x8', 'y0'], ['purple3', 'z2', 'x8', 'z2', 'x0']]
startingbrick = ['purple3', 'z2', 1, 1]

def removebrick(tempbrick, templist):
    key, *_ = templist
    return list(filter(lambda v: v[0] != key, tempbrick))

print(removebrick(leftoverbricks, startingbrick))

Output:输出:

[['purple1', 'y8', 'x0', 'y8', 'x1'], ['purple2', 'y6', 'y0', 'x8', 'y0']]

Note:笔记:

This does not modify the input list (leftoverbricks).这不会修改输入列表 (leftoverbricks)。 If you want it to be destructive then just assign the return value from this function to the appropriate variable如果您希望它具有破坏性,则只需将此函数的返回值分配给适当的变量

You can use a list comprehension to remove the elements from leftoverbricks that match the first element of startingbrick .您可以使用列表理解从leftoverbricks中删除与startingbrick的第一个元素匹配的元素。

Example:例子:

leftoverbricks = [['purple1', 'y8', 'x0', 'y8', 'x1'], ['purple2', 'y6', 'y0', 'x8', 'y0'], ['purple3', 'z2', 'x8', 'z2', 'x0']]
startingbrick = ['purple3', 'z2', 1, 1]

leftoverbricks = [brick for brick in leftoverbricks if brick[0] != startingbrick[0]]
print(leftoverbricks)

This will modify leftoverbricks in place, so you don't need to create a new list variable.这将修改leftoverbricks到位,因此您不需要创建新的列表变量。

Otuput:输出:

[['purple1', 'y8', 'x0', 'y8', 'x1'], ['purple2', 'y6', 'y0', 'x8', 'y0']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM