python - 在python中获取空格分隔的输入

标签 python python-3.x

我正在尝试获取空格分隔的输入。虽然第一个方法完全正常,但第二个方法会抛出错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

第二种方法有什么问题吗?

方法一:

x = [int(j) for j in input().split()]

方法2:

x = [j for j in int(input().split())]

最佳答案

因为您使用 split()string ,它将返回一个 list,那么您将传递这个 listint() 这就是您收到错误的原因。要更改listdatatype,您需要使用map(),如下所示或您的第一种方法。

尝试下面的代码

x = [j for j in map(int,input().split())]

关于python - 在python中获取空格分隔的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442793/

相关文章:

python - 在服务器运行时通过 django-admin 添加自定义权限

python - 如何在根 TopLevel 下的 Python Tkinter 框架中布局小部件

python - django:TypeError: 'tuple' 对象不可调用

python - 多进程: identify process in queue

python - 在类的类方法中调用 super() 以获取元类方法

javascript - 获取个人帖子id

python - 如何使用 python 和 BeautifulSoup 从网站下载 .qrs 文件?

python - 将文本 append 到文件。将文本添加到指定的 txt 文件。

python-3.x - ImportError : Please install apex from https://www. github.com/nvidia/apex 使用分布式和 fp16 训练

python - 如何对位置参数为零的函数使用 Python 多重处理?