Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

I want to convert the decimal numbers expressed in a numpy.ndarray into a matrix representing elements in fractional form, and I’ve figured out the following python code snippet:

import os 
from fractions import Fraction
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.core import Lattice, Structure, Molecule, IStructure
s=IStructure.from_file(os.path.expanduser('~') + '/pymatgen-gap-affine_matrix/EntryWithCollCode136212.cif')
a = SpacegroupAnalyzer(s, 0.1)
SymOp=a.get_symmetry_operations()
b=SymOp[1].affine_matrix.tolist()

lst=[]
for i in range(len(b)):
    lst.append([]) 
    for j in range(len(b[i])):
        print(Fraction(str(b[i][j])),end=',')
        lst[i].append(Fraction(str(b[i][j]))) 

print('\n')
print(lst)
0,-1,0,1/4,1,0,0,1/4,0,0,1,1/4,0,0,0,1,

[[Fraction(0, 1), Fraction(-1, 1), Fraction(0, 1), Fraction(1, 4)], [Fraction(1, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 4)], [Fraction(0, 1), Fraction(0, 1), Fraction(1, 1), Fraction(1, 4)], [Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 1)]]

I want to obtain the result as shown in the first form, i.e., x/y, instead of expressed as the [Fraction(x, y) function, but I still couldn’t find a way to generate the above desired results. Any hints will be highly appreciated.

See here for the related discussion.

Regards,
HZ

I think you have misread the StackOverflow post.

You don’t need Fraction(str(i)) to convert floats to fractions, and that is not what the SO post says. It uses Fraction(i) alone.

It does say you can use str(Fraction(i)) to get a string in the format "1/4".

x = 0.25
Fraction(x)       # returns Fraction(1, 4)
str(Fraction(x))  # returns '1/4' (a string)

It is not clear to me whether you want an array of strings, or an array
of Fraction objects.

What happens if you change this line?

a = SpacegroupAnalyzer(s, 0.1)

# Change to this instead:
a = SpacegroupAnalyzer(s, Fraction(1, 10))

I want to store the following format in the nested list:

[[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]

I changed to the following:

from fractions import Fraction
b=[[0.0, -1.0, 0.0, 0.25], [1.0, 0.0, 0.0, 0.25], [0.0, 0.0, 1.0, 0.25], [0.0, 0.0, 0.0, 1.0]]

lst=[]
for i in range(len(b)):
    lst.append([]) 
    for j in range(len(b[i])):
        print(Fraction(b[i][j]))
        lst[i].append(Fraction(b[i][j]))

print(lst)
0
-1
0
1/4
1
0
0
1/4
0
0
1
1/4
0
0
0
1
[[Fraction(0, 1), Fraction(-1, 1), Fraction(0, 1), Fraction(1, 4)], [Fraction(1, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 4)], [Fraction(0, 1), Fraction(0, 1), Fraction(1, 1), Fraction(1, 4)], [Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 1)]]

As you can see, if I print the elements separately, it will be displayed as the corresponding fractional form, while if I print the list in batch, the following form will be generated:

[[Fraction(0, 1), Fraction(-1, 1), Fraction(0, 1), Fraction(1, 4)], [Fraction(1, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 4)], [Fraction(0, 1), Fraction(0, 1), Fraction(1, 1), Fraction(1, 4)], [Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 1)]]