简体   繁体   English

有没有一种简单的方法可以将值转换为 integer?

[英]Is there a simple way to convert the value to integer?

a,b,c=input().split()
a=int(a)
b=int(b)
c=int(c)

for the above code is there a simple way to take in data within a single line and space-separated directly as integers?对于上面的代码,是否有一种简单的方法可以在一行中获取数据并直接以空格分隔为整数? Thanks in advance提前致谢

def main(string):
    arr = []
    while string.count(', ') > 0:
        index = string.index(', ')
        arr.append(int(string[0:index]))
        string = string[index + 2:len(string)]
    arr.append(int(string))
    return arr
print(main('1, 2, -3, 7592'))

This works by checking for a comma and a space (The indication of a new element) and adds each element to the array without the spacing as well as converting it to an integer.这通过检查逗号和空格(新元素的指示)并将每个元素添加到数组中而不带空格以及将其转换为 integer 来工作。

The best solution is to use the map method as @Asocia suggested and the code snippet is given below最好的解决方案是按照@Asocia 的建议使用map方法,下面给出了代码片段

a, b , c = map(int,input().split())

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

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