2017-09-18 2 views
0

두 개의 배열을 교차 일치시키는 작은 파이썬 프로그램에 문제가 있습니다. 부수적으로 말하자면, 요즘 파이썬으로 코딩하는 법을 배우려고합니다. 그래서 저는 여기서 약간의 실수를 저지르고 있다고 생각합니다. 기본적으로 두 개의 .txt 파일을 열고 각 요소에서 2D 배열을 만들고이를 비교하여 공통 요소가 있는지 확인하려고합니다. 지금까지,이파이썬 for 반복 행렬

#creating arrays from files 
models = np.genfromtxt('models.txt', dtype='float') 
data = np.genfromtxt('data.txt', dtype='float') 

#obtaining the number of rows in each array 
mod_nrows = models.shape[0] 
data_nrows = data.shape[0] 

#checking line by line if there are ay matches 
for i in range(mod_nrows) 
    for j in range(data_nrows) 
     do stuff.... 

같은 짓을하지만 난 문제가

mod_nrows = models.shape[0] 

는 반환하지 않습니다 될 줄 알았는데 일반 오류

File "crossmatch.py", line 27 
    for i in range(mod_nrows) 
         ^
SyntaxError: invalid syntax 

받고 있어요 int (함수 범위()의 인수 여야 함), 두 루프를 다음과 같이 변경하려고 시도했습니다.

for i in range(int(mod_nrows)) 
    for j in range(int(data_nrows)) 
     do stuff.... 

하지만 여전히 동일한 오류가 발생합니다. 어떤 제안?

답변

2

for 루프는 콜론 (:)으로 끝나야합니다.

콜론은 주로 가독성을 높이기 위해 필요합니다. 파이썬 문서가 명시 적으로 언급 됨 here

#creating arrays from files 
models = np.genfromtxt('models.txt', dtype='float') 
data = np.genfromtxt('data.txt', dtype='float') 

#obtaining the number of rows in each array 
mod_nrows = models.shape[0] 
data_nrows = data.shape[0] 

#checking line by line if there are ay matches 
for i in range(mod_nrows): 
    for j in range(data_nrows): 
     do stuff.... 
+1

지금은 다소 바보 같은 느낌 ... 많이 고마워! – Carlo