[英]How can i retrieve elements of a list between two numbers in python?
I am trying to output the elements of a list that are greater than 39, but less than 291, the desired output being 49.我试图输出大于 39 但小于 291 的列表元素,所需的输出为 49。
My Code is below, i think i have just made a small mistake as the output is almost right, if someone could help me i would appreciate it as python is new to me.我的代码如下,我想我刚刚犯了一个小错误,因为输出几乎是正确的,如果有人能帮助我,我会很感激,因为 python 对我来说是新手。
list=[1,2,30,49,29,299,291,39,30]
for x in list:
if x > 39 & x < 291:
print(x)
here's an efficient way to do it without having to confuse yourself with and
and &
这是一种有效的方法,无需将自己与and
和&
混淆
lst=[1,2,30,49,29,299,291,39,30]
[print(x) for x in lst if 39 < x < 291]
note: you shouldn't use
list
as a variable name since that's the name of a casting operation;注意:你不应该使用list
作为变量名,因为它是转换操作的名称; eglist()
例如list()
You're using &
instead of and
, which won't work (Python will throw an error).您正在使用&
而不是and
,这将不起作用(Python 会抛出错误)。
list=[1,2,30,49,29,299,291,39,30]
for x in list:
if x > 39 and x < 291:
print(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.