Python AttributeError: 'int' 对象没有属性 'split'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28769921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'int' object has no attribute 'split'
提问by user28011994
I'm doing the merge sort in python but I have a problem. When I try to insert the list from the console (one number per line which return a list of string) I cannot convert it in integers. Can you help me understanding the problem.
我正在 python 中进行合并排序,但我遇到了问题。当我尝试从控制台插入列表(每行一个数字,返回字符串列表)时,我无法将其转换为整数。你能帮我理解这个问题吗。
import sys
def mergeSort(lista):
res = []
for i in lista[0].split():
res.append(int(i))
if len(res)>1:
mid = len(res)//2
lefthalf = res[:mid]
righthalf = res[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i<len(lefthalf) and j<len(righthalf):
if lefthalf[i]<righthalf[j]:
res[k]=lefthalf[i]
i=i+1
else:
res[k]=righthalf[j]
j=j+1
k=k+1
while i<len(lefthalf):
res[k]=lefthalf[i]
i=i+1
k=k+1
while j<len(righthalf):
res[k]=righthalf[j]
j=j+1
k=k+1
print(res)
alist = []
for l in sys.stdin:
alist.append(l.strip())
mergeSort(alist)
The code error says: AttributeError: 'int' object has no attribute 'split' The input is a file (given from the shell with the command: "python3 merge.py < data.txt") with the list of numbers one per line. Example: 2 3 0 12 11 7 4 Should return 0 2 3 4 7 11 12 Of course I don't have an output because of the error
代码错误说: AttributeError: 'int' object has no attribute 'split' 输入是一个文件(从 shell 使用以下命令提供:“python3 merge.py < data.txt”),其中包含每行一个的数字列表. 示例:2 3 0 12 11 7 4 应该返回 0 2 3 4 7 11 12 当然我没有输出因为错误
采纳答案by Iguananaut
You're converting a list of strings to a list of integers at the top of your mergeSortroutine. The subsequent recursive calls to mergeSortare trying to do the same, except now to lists of integers.
您正在mergeSort例程顶部将字符串列表转换为整数列表。随后的递归调用mergeSort试图做同样的事情,除了现在是整数列表。
You should handle all file parsing completely separate from your mergeSortroutine which should be designed just to work on a list of numbers. This is a "separation of concerns".
您应该完全独立于您的mergeSort例程来处理所有文件解析,而例程应该设计为仅用于处理数字列表。这是一种“关注点分离”。
回答by Paul Lo
It should be for i in listarather than for i in lista[0].split(), and you can simply achieve it by list comprehension: res = [int(num) for num in lista]
它应该是for i in lista而不是for i in lista[0].split(),你可以简单地通过列表理解来实现它:res = [int(num) for num in lista]
回答by Curtis Mattoon
If you also want the index, you can use enumerate:
如果您还需要索引,可以使用enumerate:
data = ['itemA', 'itemB', 'itemC', 'itemD']
for (i, item) in enumerate(data):
print("Item #%d is %s" % (i, str(item)))
For future reference, you can debug like so:
为了将来参考,您可以像这样调试:
def mergeSort(lista):
res = []
print(lista)
for i in lista[0].split():
print(i)
res.append(int(i))
回答by Rahul Sahotay
You're running this code in Python 2 and you only entered a single number. If that's true, replace input with raw_input and it should work.
您在 Python 2 中运行此代码并且只输入了一个数字。如果这是真的,用 raw_input 替换输入,它应该可以工作。

