python - 如何拆分字符串并转换为整数

标签 python string

<分区>

numbers = "51-52"
for st in numbers:
    part = st.split("-")
    print(part)

到目前为止,这是我的代码。

我的结果:['5']['1']['', '']['5']['2']

预期:['51']['52']

最佳答案

numbers = "51-52"
part = [int(x) for x in numbers.split("-")]
print(part)

关于python - 如何拆分字符串并转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37515372/

相关文章:

python - 'numpy.float64'对象的特殊条件不可调用

python - 如何使用 Pandas Timestamp 折叠参数?

python - 我怎样才能使这个 float 格式更干净?

c# - MeasureString 和 DrawString 的区别

c++ - 不会等待 getline C++

ios - 如何在 Swift 中从文件扩展名中拆分文件名?

Python for 循环优化

python - 从字符串中删除范围内的字符

python - 解压缩通用数组的 numpy 数组形状

从 fortran 调用 C(ifort、gfortran)