我尝试在 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
) 正确,并且它打印预期的数字。
现在您将得到大于 10 的数字,只需更改您的输入列表即可)
numbers = [11, 12, 13, 14, 15, 16, 17, 18, 19]
filtered_numbers = [x for x in numbers if x > 10]
print(filtered_numbers)