2016-07-19 5 views
-1

이후 작업을 위해 for 루프의 각 반복 출력을 작성하려고합니다. 여기 내 코드파이썬에서 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는 엄청난 양의 데이터를 포함)

40 
1 
4 
3 
5 
7 
29 
6 
9 
4 
7 
3 
50 
1 
2 
3 
4 
5 
57 
9 
8 
7 
6 
5 

값 nClus = 4 nEig = 5 진행 방법에 대한 제안 사항이 있으십니까?

+0

for 루프 내에 'print' 문이 있으므로 출력 작성시 잘 수행해야합니다. 네가 원하는 걸하지 않는거야? 그것은 무엇을하고 있으며, 무엇을하고 싶습니까? – Kevin

+0

팁 :'content = f.readlines() '를 사용하여 메모리에서 먹는 대신'for line in f' –

+0

@Kevin 각 출력을 변수에 저장하여 필자가 원하는대로 호출 할 수 있도록하고 싶습니다. 내 추가 작업 – ratamboli

답변

0

배열 (mydata 아래)에 저장하지 않는 이유는 무엇입니까? 나는 j이 어디에서 멈추는 지 알지 못합니다 (other_dimension, 결과의 차원이 하나 뿐이지 만 배열 크기를 알 수 없으면 삭제할 수 있습니다). 그러나이 형식을 따라 데이터를 저장하기 위해 숫자 배열을 얻을 수 있습니다 to :

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 
관련 문제