将列表除以输出为分数的列表

Dividing a list by a list with output as fractions

我想用一个整数列表除以另一个整数列表,输出以分数形式给出(不是小数)。从问题"How to combine elements from two lists into a third?", I found a great answer,具体来说:

a  =   [3,    6,   8,   65,   3]
b  =   [34,   2,   5,   3,    5]
c = [float(x)/y for x,y in zip(a,b)]
print(c)

唯一的问题是这段代码输出的是小数,而我正在寻找分数。我怀疑我需要使用像 float 这样的术语,但对于分数而不是小数?我也可以尝试将上述代码的输出转换为分数,但我怀疑由于重复小数的不精确性,这会更复杂。

进一步说明:我要使用的两个列表都会产生需要减少的分数。我的列表每个有 49 个项目,但为了更简洁地显示代码,我将它们减少到每个 10 个。

a = [6198089008491993412800, 3099044504245996706400, 2066029669497331137600, 2324283378184497529800, 1239617801698398682560, 2066029669497331137600, 1328161930391141445600, 1549522252122998353200, 1033014834748665568800, 1549522252122998353200]
b = [3099044504245996706400, 2066029669497331137600, 2324283378184497529800, 1239617801698398682560, 2066029669497331137600, 1328161930391141445600, 1549522252122998353200, 1033014834748665568800, 1549522252122998353200, 1126925274271271529600]
c = [float(x)/y for x,y in zip(b,a)]

print(c)

[0.5, 0.6666666666666666, 1.125, 0.5333333333333333, 1.6666666666666667, 0.6428571428571428, 1.1666666666666667, 0.6666666666666666, 1.5, 0.7272727272727272]

我要找的结果是:

1/2, 2/3, 9/8, 8/15, 5/3, 9/14, 7/6, 2/3, 3/2, 8/11

您可以使用 fractions 模块:

import fractions

a = [
    6198089008491993412800,
    3099044504245996706400,
    2066029669497331137600,
    2324283378184497529800,
    1239617801698398682560,
    2066029669497331137600,
    1328161930391141445600,
    1549522252122998353200,
    1033014834748665568800,
    1549522252122998353200,
]

b = [
    3099044504245996706400,
    2066029669497331137600,
    2324283378184497529800,
    1239617801698398682560,
    2066029669497331137600,
    1328161930391141445600,
    1549522252122998353200,
    1033014834748665568800,
    1549522252122998353200,
    1126925274271271529600,
]

for x, y in zip(b, a):
    print(fractions.Fraction(x, y))

打印:

1/2
2/3
9/8
8/15
5/3
9/14
7/6
2/3
3/2
8/11