简体   繁体   English

Python-3.x:使用以下代码时出现错误,因为“对象没有属性'split''”

[英]Python-3.x: while using the following code i got an error as “object has no attribute 'split ”'

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

this is the code i used. 这是我使用的代码。 I want to get list of input from the user but i am getting error as 我想从用户那里获取输入列表,但由于出现错误

    arr = [int(x) for x in input.split()]
    AttributeError: 'builtin_function_or_method' object has no attribute 'split'

You are calling split on the builtin function input . 您正在对内置函数input调用split。 While what you should be doing is calling it on the n variable. 您应该在n变量上调用它。 So it should look like this 所以应该看起来像这样

n = int(input()) 
arr = [int(x) for x in n.split()] # Still wrong

But still let me quote that int however has no split attribute. 但是我还是要引用int但是没有split属性。 So make sure it is a String type. 因此,请确保它是String类型。

So your try not to convert your input into int until you process the value. 因此,在处理该值之前,请勿尝试将输入转换为int

You're trying to split the input function itself, and not its results. 您试图拆分输入函数本身,而不是结果。 You need to split the string it returns, like this input().split() . 您需要分割返回的字符串,例如此input().split() Mind you that this will not do anything because split has no parameters. 请注意,这不会做任何事情,因为split没有参数。 It'll simply return a list with the user's input as the sole parameter. 它只会返回一个列表,其中用户输入是唯一的参数。 If you want to divide the input string into its chars, you can simply use list(input()) . 如果要将输入字符串分成其字符,则可以简单地使用list(input())

I believe this is what you are going for, turn the input to a list and ints for items in the list 我相信这就是您要的目的,将输入内容转换为列表,并为列表中的项目输入整数

  l = [int(i) for i in list(input('Enter number: '))]
 Enter number: 101 [1, 0, 1] 

As Vineeth said, you are trying to call split on input. 正如Vineeth所说,您正在尝试在输入时调用split。 Also, you can't split an integer since split is a method that can only be used on strings, so you shouldn't cast n as an integer. 另外,您不能拆分整数,因为split是只能在字符串上使用的方法,因此不应将n强制转换为整数。

n = str(input())
arr = [int(x) for x in n.split()]

暂无
暂无

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

相关问题 以下代码给了我一个错误:“list”对象没有属性“split” - The following code is giving me an error: 'list' object has no attribute 'split' python-3.x出现错误的hyde - Error hyde with python-3.x 运行以下代码时出现错误('DataFrame' 对象没有属性 'as_matrix') - I am getting error('DataFrame' object has no attribute 'as_matrix') while running following code 对于 python 中的以下代码,我收到错误--AttributeError: 'str' object has no attribute 'next' - For the following code in python, I am getting the error--AttributeError: 'str' object has no attribute 'next' 如何在 python-3.x 中使用字典格式化字符串? - How do I format a string using a dictionary in python-3.x? 尝试使用 Python 和 OpenCV 加载图像时,我在以下代码中收到“属性错误”? - I am getting 'Attribute Error' in the following code while tried loading an image, using Python & OpenCV? 逻辑回归; 我收到以下错误:'DataFrame' 对象没有属性 'name' - Logistic Regression; I got the following error : 'DataFrame' object has no attribute 'name' 我收到此错误 AttributeError: 'function' object has no attribute 'hlauncher' while试图从另一个文件中获取属性 - I got this error AttributeError: 'function' object has no attribute 'hlauncher' while trying to get a attribute from another file array() 错误 python-3.x 中的 function - Error with array() function in python-3.x 当我尝试运行以下代码时,我收到此错误 'str' object has no attribute 'text' - I receive this error 'str' object has no attribute 'text' when I try to run the following code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM