简体   繁体   English

如何在python中检索两个数字之间的列表元素?

[英]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作为变量名,因为它是转换操作的名称; eg list()例如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.

相关问题 如何优化具有两个元素的列表之间的交集,并在python中生成没有重复的列表列表? - How can I optimize the intersections between lists with two elements and generate a list of lists without duplicates in python? 如何交换python列表中的两个元素? - How can I exchange two elements in a python list? 如何在python的列表元素中拆分两个字符? - How can I split between two characters in a list element in python? 如何在 Python 的列表中的两个元素之间找到缺失的元素? - How to find missing elements between two elements in a list in Python? 如何在Python的列表中重复查找两个元素之间的所有元素? - How to find all elements between two elements repeatedly in a list in Python? Python总和两个数字之间的元素 - Python sum elements between two numbers 如何在Python列表中的两个连续数字之间插入一个数字 - How to insert one number between two consecutives numbers in a list in Python 我该如何列出两个数字之间恒定递增的n +1个值? - How can I make a list of n + 1 values with constant increment between two numbers? 如何在Python中同一行上的两个单词之间设置分隔符而不设置两个数字之间的分隔符? - How can I set delimiters between two numbers without setting the delimiter between two words on the same line in Python? 如何找出 Python 中两个数字之间的哪个数字不同? - How can I find out which digit is different between two numbers in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM