列表理解以查找列表中每个数字的所有倍数小于数字

The*_*ark 4 python list-comprehension

我正在尝试编写一个函数,该函数将查找列表中至少有一个数字的倍数的所有数字,其中倍数小于某个数字.这是我到目前为止所尝试的:

def MultiplesUnderX(MultArray,X):
    '''
    Finds all the multiples of each value in MultArray that
    are below X.
    MultArray: List of ints that multiples are needed of
    X: Int that multiples will go up to
    '''
    return [i if (i % x == 0 for x in MultArray) else 0 for i in range(X)]
Run Code Online (Sandbox Code Playgroud)

例如,MultiplesUnderX([2,3],10)将返回[2,3,4,6,8,9].我有点不确定如何使用列表理解中的for循环来完成此操作.

Aja*_*234 8

您可以使用Python any()函数来检查MultArray中是否至少有一个分隔符实例:

def MultiplesUnderX(MultArray,X):

    return [i for i in range(X) if any(i % x == 0 for x in MultArray)]
Run Code Online (Sandbox Code Playgroud)