![]() |
Find max and min integers - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Find max and min integers (/thread-11721.html) |
Find max and min integers - mmaz67 - Jul-23-2018 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 RE: Find max and min integers - gontajones - Jul-23-2018 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.
RE: Find max and min integers - buran - Jul-23-2018 (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 |