Home > database >  Most pythonic way to split string into integers
Most pythonic way to split string into integers

Time:01-28

I have a string that needs to be validated that it is of the form of ‘#-#’ where the #s should be unsigned integers. What is the most pythonic way to split this into two integers, lo and hi, possibly including type-checking/validation that lo < hi, unsigned ints, and so on?

I’ve thought about various solutions, but I have varying criteria and it must make sure that # and # are unsigned ints with only the one hyphen in between.

CodePudding user response:

I'm not sure if this is absolutely the most pythong way, but I'd split on the - character and use a list comprehension to generate lo and hi:

[lo, hi] = [int(x) for x in s.split('-')]

CodePudding user response:

You can try something like this:

string = "43-3" #example data
num = string.split("-") #split the string

lo = int(num[0])
hi = int(num[1])

if(lo<hi):
  print("ok")
else:
  print("ko")

this will work as long your string is exactly as you told us and there are only unsigned ints in the string.

CodePudding user response:

Best I can think of is using a regular expression to match the exact pattern and assert that everything is correct. Note however that doing it this way will crash the program as soon as an invalid input appears, unless you do some extra error handling.

import re

input_strings = [
    "123-321",
    "92646-278364",
    # "12345-123",  # error: low > high
    # "123.45-98765",  # error: not integers
]

def get_num_pair(num_str: str):
    m = re.match(r'(\d )-(\d )', num_str)
    assert m and len(m.groups()) == 2
    lo, hi = int(m.group(1)), int(m.group(2))
    assert 0 <= lo <= hi
    return lo, hi

for input_str in input_strings:
    print(get_num_pair(input_str))

CodePudding user response:

Expanding on Mureinik's answer - I'd suggest you do the following:

  • use a list comprehension to split the list on '-' and turn each bit into ints
  • carry out validation on the resulting list to ensure it meets your requirements - exactly two elements and the first one is lower
  • wrap this in a try, except block in case the list comprehension gives an error
  • wrap that in a function that encapsulates the concept of splitting #-# strings

e.g.

def parse_twoval_str(twoval_str):
    try:
        vals = [int(s) for s in twoval_str.split("-")]

    except ValueError:
        raise ValueError("Could not parse input - must be of form '123-234'")

    if len(vals) != 2:
        raise ValueError("Input must contain two values")

    a, b = vals

    if a > b:
        raise ValueError("First value must be less than second value")

    return a, b
  • Related