用python编写for循环的输出

用python编写for循环的输出,python,for-loop,output,Python,For Loop,Output,我正在尝试编写for循环的每个迭代输出,以便进行进一步的操作。 这是我的密码 #!/usr/bin/python import io from operator import itemgetter with open('test.in') as f: content = f.readlines() content = [int(x) for x in content] content = tuple(content) nClus = input("Number

我正在尝试编写for循环的每个迭代输出,以便进行进一步的操作。 这是我的密码

#!/usr/bin/python

import io
from operator import itemgetter
with open('test.in') as f:
    content = f.readlines()
    content = [int(x) for x in content]
    content = tuple(content)
    nClus = input("Number of Clusters: ")
    nEig = input("Number of eigen values: ")
    j = 0
    k = nClus + 1
    content1 = ""
    for i in range(1,k):
        print content[j*(nEig+1):i*(nEig+1)]
        j = j + 1
文件test.in如下所示(这是一个示例,实际test.in包含大量数据)

数值nClus=4,nEig=5。
有关于如何继续的建议吗?

为什么不将它们保存到数组中(
mydata
下面)?我看不到
j
在哪里停止(
other_dimension
,如果您只有一个维度的结果,您可能只需要删除它,我不知道您的数组大小),但您可以按照此格式获取numpy数组以将数据保存到:

import numpy as np
... [your code]
    mydata = np.zeros([k,other_dimension]) // other_dimension only if you are saving a rectangular matrix of results instead of a vector
    for i in range(1,k):
        mydata[row, column] = content[j*(nEig+1):i*(nEig+1)] // put your iterators here for row, column if applicable (rectangular matrix), otherwise mydata[iterator]
        print mydata[k, other_dimension] // other_dimension only if you are saving a rectangular matrix of results instead of a vector
        j = j + 1

在for循环中有一个
print
语句,这样在编写输出时就可以了。它不是在做你想做的吗?它在做什么?您希望它做什么?提示:
用于f中的line
而不是使用
content=f.readlines()
@Kevin消耗您的内存。我希望将每个输出保存到一个变量中,以便根据需要调用其中的任何一个,以便进行进一步的操作
import numpy as np
... [your code]
    mydata = np.zeros([k,other_dimension]) // other_dimension only if you are saving a rectangular matrix of results instead of a vector
    for i in range(1,k):
        mydata[row, column] = content[j*(nEig+1):i*(nEig+1)] // put your iterators here for row, column if applicable (rectangular matrix), otherwise mydata[iterator]
        print mydata[k, other_dimension] // other_dimension only if you are saving a rectangular matrix of results instead of a vector
        j = j + 1