Python - 返回一个比另一个更大的值?

Kev*_*vvv 0 python

def all_gt(nums, n):
    i = []
    for c in nums:
        if c > n:
            i += c
    return i
Run Code Online (Sandbox Code Playgroud)

这是我使用的代码,'i'应该返回大于n的nums值.但我的支架内没有任何返回.例如,

all_gt([1,2,3,4], 2) => [3,4]

谁知道如何解决?谢谢

Lev*_*von 5

你声明i是一个列表,所以你需要append它而不是添加.

def all_gt(nums, n):
    i = []
    for c in nums:
        if c > n:
            i.append(c)  ## <----- note this
    return i
Run Code Online (Sandbox Code Playgroud)

或者,您可以这样做:

            i += [c]
Run Code Online (Sandbox Code Playgroud)

代替追加.