Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find max and min integers
#1
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
Reply
#2
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.
Reply
#3
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  read a text file, find all integers, append to list oldtrafford 12 3,515 Aug-11-2022, 08:23 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020