简体   繁体   中英

Lower and Upper Bound Integers, Python

I have to write a function that takes a list of integers, an upper bound, and a lower bound and return a list that has only integers from the argument list that are within the upper and lower bounds (inclusive).

list = [1,2,3,4,5,6,7,8,9,10]
def getMembersInRange(i):
    for i in range(1,5):
        print (i)
    for i in range(6,10):
        print(i)

This is what I tried, but it's not giving me the output desired.

A simple list comprehension will serve your purposes.

>>> k = [1,2,3,4,5,6,7,8,9,10]
>>> upper = 7
>>> lower = 3
>>> [i for i in k if lower <= i <= upper]
[3, 4, 5, 6, 7]

PS: please don't use Python keywords for your variable names... it will cause you trouble, guaranteed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM