Determining whether a variable is less/greater than a range.

  • Thread starter simonharrison.uk
  • Start date
S

simonharrison.uk

Hi. I'm having another go at learning Python so I'll probably be
asking a few basic questions. Here is the first one.

a = list(range(10, 21)

b = 9

c = 21

How can I find out if b and c have values less or more than the values
in list a?

Thanks.
 
S

simonharrison.uk

Found it. min and max functions. I thought that this would be
implemented as a list method:

a.min
a.max

I can see that the built in functions make sense.
 
E

eric

Hi. I'm having another go at learning Python so I'll probably be
asking a few basic questions. Here is the first one.

a = list(range(10, 21)

b = 9

c = 21

How can I find out if b and c have values less or more than the values
in list a?

Thanks.


what about:
a = list(range(10, 21))


def compare(value, list):
m,M = min(list), max(list)
return value>=m, value<=M

b = 9
c = 21

print compare(b,a)
print compare(c,a)

for i in range (8, 25):
print i, compare(i, a)


"""
(False, True)
(True, False)
8 (False, True)
9 (False, True)
10 (True, True)
11 (True, True)
12 (True, True)
13 (True, True)
14 (True, True)
15 (True, True)
16 (True, True)
17 (True, True)
18 (True, True)
19 (True, True)
20 (True, True)
21 (True, False)
22 (True, False)
23 (True, False)
24 (True, False)
"""

it helps?
 
P

Peter Otten

Hi. I'm having another go at learning Python so I'll probably be
asking a few basic questions. Here is the first one.

a = list(range(10, 21)

b = 9

c = 21

How can I find out if b and c have values less or more than the values
in list a?

Are there any items in a that are less than b or greater than c?
False

No. Are there any items in a that are greater than b?
True

Yes.

Peter
 
T

Tim Chase

a = list(range(10, 21))
b = 9

c = 21

How can I find out if b and c have values less or more than the values
in list a?

Sounds like a good use for 2.5's addition of the any() and all()
functions -- you don't mention whether you want your variable
compared against *any* of the list items or *all* of the list
items. You also don't mention whether you want the comparison
results, or just a single scalar. Lastly, you omit the details
of what happens if your target value ("d" below) falls within the
range. One of the following should do it for you:
False

If you just want the comparisons:

y1 = [b<x for x in a]
y2 = [c<x for x in a]
y3 = [d<x for x in a]
z1 = [(b<x, b>x) for x in a]
z2 = [(c<x, c>x) for x in a]
z3 = [(d<x, d>x) for x in a]

-tkc
 
S

simonharrison.uk

a = list(range(10, 21))
How can I find out if b and c have values less or more than the values
in list a?

Sounds like a good use for 2.5's addition of the any() and all()
functions -- you don't mention whether you want your variable
compared against *any* of the list items or *all* of the list
items.  You also don't mention whether you want the comparison
results, or just a single scalar.  Lastly, you omit the details
of what happens if your target value ("d" below) falls within the
range.  One of the following should do it for you:

   >>> a = range(10,21)
   >>> b = 9
   >>> c = 21
   >>> d = 15
   >>> any(b < x for x in a)
   True
   >>> all(b < x for x in a)
   True
   >>> any(c < x for x in a)
   False
   >>> any(d < x for x in a)
   True
   >>> all(d < x for x in a)
   False
   >>> any(c > x for x in a)
   True
   >>> all(c > x for x in a)
   True
   >>> any(d > x for x in a)
   True
   >>> all(d > x for x in a)
   False

If you just want the comparisons:

   y1 = [b<x for x in a]
   y2 = [c<x for x in a]
   y3 = [d<x for x in a]
   z1 = [(b<x, b>x) for x in a]
   z2 = [(c<x, c>x) for x in a]
   z3 = [(d<x, d>x) for x in a]

-tkc

Hi Tim. I'm just learning Python at the moment. All I was really
wanting was a True or False value which min and max give.

a = list(range(10, 21))

b = 9

c = 21

if b < min(a)
if c > max(a)

The any() and all() functions look useful so thanks for the tips.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top