Python 列表理解的问题:为什么我得到一个空列表?

问题描述 投票:0回答:1

我尝试在 Python 中使用列表理解来过滤数字列表,但结果得到一个空列表。这是我的代码:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
filtered_numbers = [x for x in numbers if x > 10]
print(filtered_numbers)

我希望

filtered_numbers
列表包含
numbers
列表中大于 10 的数字。但是,我得到的输出是一个空列表:
[]

我已经仔细检查了我的列表理解语法并尝试了不同的条件表达式,但我一直得到一个空列表。我还尝试在列表理解之前打印列表,以确保我的输入列表 (

numbers
) 正确,并且它打印预期的数字。

python conditional-statements filtering list-comprehension
1个回答
0
投票

现在您将得到大于 10 的数字,只需更改您的输入列表即可)

numbers = [11, 12, 13, 14, 15, 16, 17, 18, 19]
filtered_numbers = [x for x in numbers if x > 10]
print(filtered_numbers)
© www.soinside.com 2019 - 2024. All rights reserved.