我正在尝试获取空格分隔的输入。虽然第一个方法完全正常,但第二个方法会抛出错误:
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
,那么您将传递这个 list
到 int()
这就是您收到错误的原因。要更改list
的datatype
,您需要使用map()
,如下所示或您的第一种方法。
尝试下面的代码
x = [j for j in map(int,input().split())]
关于python - 在python中获取空格分隔的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442793/