Python Forum

Full Version: Find max and min integers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am currently working on regex string matching in order to find line with 2 digits:

Example:
[10:3] abc

I am expecting the output will write in file as: 10&3
However, the output shown is: 10&,
It seems that the thought that comma (,) is the min value. Is there any way to get the correct max and min?
  if re.match(r"\[-?\d*:?-?\d*\]\s*\S*", str):
            aa = re.findall(r'\d+',str)
            ab= ",".join(aa)
            high = max(ab)
            low = min(ab)
            result = f.writelines(high + "&" + low)
            return result
In your code you can get max() and min() from aa.

import re


s = '[10:3] abc'
if re.match(r"\[-?\d*:?-?\d*\]\s*\S*", s):
    aa = re.findall(r'\d+', s)
    high = max(aa)
    low = min(aa)
    print(high + "&" + low)
And be careful when use min() and max() with strings, they check every character as one digit.

>>> s = '54321'
>>> max(s)
'5'
>>> min(s)
'1'
>>> a = '10'
>>> b = '3'
>>> s = ''.join([a,b])
>>> s
'103'
>>> max(s)
'3'
>>> min(s)
'0'
>>>
For the last, don't use str as a variable name. You are overriding the python str() bultin function.
(Jul-23-2018, 10:05 AM)gontajones Wrote: [ -> ]they check every character as one digit.
the don't check it as digit. the order is evaluated based on ascii order, so even that is possible
>>> 'a'>'1'
True