简体   繁体   English

Pythonic方式在列表推导中使用第二个条件

[英]Pythonic way to use the second condition in list comprehensions

Let's assume the following function: 我们假设以下函数:

def myfun(my_list, n, par1=''):
    if par1 == '':
        new_list = [[my_fun2(i,j) for j in range(n)] for i in range(n)]
    else:
        new_list = [[my_fun2(i,j) for j in range(n)] for i in range(n) if my_fun2(i,n) == par1]
    return new_list

As you can see, there are two different scenarios depending on par1 . 如您所见,根据par1 ,有两种不同的场景。 I do not like that line 3 and line 5 are almost identical and do not follow the DRY (Don't Repeat Yourself) principle. 我不喜欢第3行和第5行几乎相同,并且不遵循DRY(不要重复自己)原则。 How can this code be improved? 如何改进此代码?

This might work: 这可能有效:

new_list = [[my_fun2(i,j) for j in range(n)] for i in range(n) if par1 == '' or my_fun2(i,n) == par1]

So used like this: 所以像这样使用:

def myfun(my_list, n, par1=''):
    return [
               [my_fun2(i,j) for j in range(n)]
               for i in range(n) if par1 == '' or my_fun2(i,n) == par1
           ]

You could choose the condition function dynamically by using a function that just returns True in the first case and one that actually compares the my_fun2 result with par1 in the second case: 您可以通过使用在第一种情况下仅返回True的函数和在第二种情况下实际将my_fun2结果与par1进行比较的函数来动态选择条件函数:

def myfun(my_list, n, par1=''):
    if par1 == '':
        cond = lambda x, y: True
    else:
        cond = lambda i, n: my_fun2(i, n) == par1
    return [[my_fun2(i,j) for j in range(n)] for i in range(n) if cond(i,n)]

Or by replacing the outer loop with a generator expression in case par1 isn't an empty string: 或者,如果par1不是空字符串,则用生成器表达式替换外部循环:

def myfun(my_list, n, par1=''):
    if par1 == '':
        outer = range(n)
    else:
        # a conditional generator expression
        outer = (i for i in range(n) if my_fun2(i,n) == par1)
    return [[my_fun2(i,j) for j in range(n)] for i in outer]

However don't let DRY make the function harder to read, maintain or debug. 但是,不要让DRY使函数更难以读取,维护或调试。 I, personally, think that your approach is fine (and probably faster) and you probably shouldn't change anything. 我个人认为你的方法很好(可能更快),你可能不应该改变任何东西。

why not use a filter ? 为什么不使用过滤器?

from operator import eq
def myfun(my_list, n, par1=''):
    new_list = ([my_fun2(i,j) for j in range(n)] for i in range(n))
    if par1 != '':
        new_list = filter(eq(par1),new_list)
    return list(new_list)

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

相关问题 将列表推导用于副作用是否是 Pythonic? - Is it Pythonic to use list comprehensions for just side effects? 有没有一种pythonic方法来重写以下代码块? 列举理解? - Is there a pythonic way to re-write the following block of code? List comprehensions? 确定列表中第一个真实条件的pythonic方法 - pythonic way to identify first true condition in a list 如果满足条件,修改列表元素的Python方法 - Pythonic way to modify list element if condition is met 使用if条件的更有效的pythonic方式 - A more efficient pythonic way to use if condition 使用列表推导使函数更加pythonic - Using list comprehensions to make a funcion more pythonic 如何使用map或filter而不是list comprehensions过滤特定值的嵌套字典(pythonic方式)? - How to filter a nested dictionary (pythonic way) for a specific value using map or filter instead of list comprehensions? 有没有更 Pythonic 的方式来使用多个列表理解? - Is there a more pythonic way to use multiple list comprehension? 什么是将字典中元组的第二个 object 收集到列表中的最pythonic方式 - Whats most pythonic way of colelcting second object of tuple in dictionary into a list 大多数Pythonic方法在第二个对象中使用来自一个脚本的变量 - Most Pythonic way to use a variable from one script in a second object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM