如何在python中拆分整数输入? [英] How to split integer input in python?

查看:370
本文介绍了如何在python中拆分整数输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你这样写

n = str(input())

n = n.split()

print(n)

那行得通.但是,如果您尝试使用整数进行操作,则会得到

`Value Error`.

如何使用int类型?

解决方案

是否要分隔几个数字? 1 2 3-> [1、2、3]

n = str(input())
n = n.split()
numbers = [int(i) for i in n]
print(numbers)

还是将数字拆分为数字? 123-> [1、2、3]

n = str(input())
numbers = [int(i) for i in n]
print(numbers)

如果要用定界符1%3-> [1、3]分割数字,请使用Nikhil答案.

If you write like

n = str(input())

n = n.split()

print(n)

That will work. But if you try to do it with integers, you will get

`Value Error`.

How to do it with int type?

解决方案

Do you want to separate several numbers? 1 2 3 -> [1, 2, 3]

n = str(input())
n = n.split()
numbers = [int(i) for i in n]
print(numbers)

Or split a number in numeral? 123 -> [1, 2, 3]

n = str(input())
numbers = [int(i) for i in n]
print(numbers)

Use Nikhil answer, if you want to split a number with delimiters 1%3 -> [1, 3]

这篇关于如何在python中拆分整数输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆