[Solved] Separate odd and even numbers into different output files [closed]


This is how you could do it:

import numpy as np

a = np.loadtxt("test.txt")

odd = []
even = []

for ele in a:
    if ele % 2 == 0:
        even.append(ele)
    else:
        odd.append(ele)

First you read a file using a = np.loadtxt("test.txt"), then you sort the contents into two arrays even and odd and finaly you save the files using np.savetxt("odd.txt", odd) and np.savetxt("even.txt", even)

Edit: As suggested by jonrsharpe you could do it more efficiently as follows:

import numpy as np

a = np.loadtxt("test.txt")

np.savetxt("odd.txt", a[a%2==1])
np.savetxt("even.txt", a[a%2==0])

3

solved Separate odd and even numbers into different output files [closed]