0

I have a problem with a small python program to cross-match two arrays. As a side note, I'm learning to code in Python in these days, so I assume I'm making some incredibly trivial mistake here. Basically, I want to open two .txt files, create a 2D array from each of them, and compare them to see if they have common elements. So far, I did something like this

#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....

but I'm getting the generic error

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

I thought the problem might be that

mod_nrows = models.shape[0]

doesn't return an int (which should be the argument of the function range()), and I tried to change the two for loops to

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

but I'm still getting the same error. Any suggestions?

BioGeek
23k23 gold badges91 silver badges156 bronze badges
asked Sep 18, 2017 at 14:04
0

1 Answer 1

2

Any for loop should end with a colon (:).

The colon is required primarily to enhance readability. Python docs explicitly mention 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....
answered Sep 18, 2017 at 14:06
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.