简体   繁体   English

将字符串拆分为整数的大多数 pythonic 方法

[英]Most pythonic way to split string into integers

I have a string that needs to be validated that it is of the form of '#-#' where the #s should be unsigned integers.我有一个字符串需要验证它是否为“#-#”形式,其中#s 应该是无符号整数。 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?将其拆分为两个整数 lo 和 hi 的最 pythonic 方法是什么,可能包括 lo < hi 的类型检查/验证、无符号整数等?

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.我考虑过各种解决方案,但我有不同的标准,它必须确保 # 和 # 是无符号整数,中间只有一个连字符。

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 :我不确定这是否绝对是pythong 的方式,但我会在-字符上拆分并使用列表理解来生成lohi

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

Best I can think of is using a regular expression to match the exact pattern and assert that everything is correct.我能想到的最好的方法是使用正则表达式来匹配确切的模式并assert一切都是正确的。 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))

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.只要您的字符串与您告诉我们的完全一致并且字符串中只有无符号整数,这就会起作用。

Expanding on Mureinik's answer - I'd suggest you do the following:扩展 Mureinik 的回答 - 我建议您执行以下操作:

  • use a list comprehension to split the list on '-' and turn each bit into int s使用列表推导式将列表拆分为'-'并将每一位转换为int s
  • 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将其包装在try中, except阻止以防列表理解给出错误
  • wrap that in a function that encapsulates the concept of splitting #-# strings包装在 function 中,封装了拆分#-#字符串的概念

eg例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM