1

I want to divide one list of integers by another list of integers, with the output given as fractions (not decimals). From the question "How to combine elements from two lists into a third?", I found a great answer, specifically:

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)

The only problem is that this code outputs decimals and I'm looking for fractions. I suspect I need to use term like float, but for fractions instead of decimals? I could also try to convert the output of the above code to fractions, but I suspect that would be more complicated because of the imprecision of repeating decimals.

Further notes: the two lists I want to use all will produce fractions which require reduction. My lists have 49 items each, but to show the code more concisely, I've reduced them to 10 each.

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]

The result I am looking for is:

1/2, 2/3, 9/8, 8/15, 5/3, 9/14, 7/6, 2/3, 3/2, 8/11
not_speshal
  • 22,093
  • 2
  • 15
  • 30
Jerry
  • 21
  • 2
  • 8
    Did you look at https://docs.python.org/3/library/fractions.html? – jonrsharpe Aug 17 '21 at 21:36
  • 1
    @j1-lee The end of the selected answer shows using a numerator and denominator. – Mark Tolonen Aug 17 '21 at 21:45
  • @j1-lee - The exact same logic still applies. – not_speshal Aug 17 '21 at 21:45
  • 1
    Welcome to Stack Overflow. Next time please start with [at least a little research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Literally all you needed to do is put `python fractions` [into a search engine](https://duckduckgo.com/?q=python+fractions) and you would get multiple high-quality answers from the Internet (including the official documentation for built-in standard library support) right off the top. – Karl Knechtel Aug 17 '21 at 21:56
  • It also helps to narrow your question to the part that you're actually concerned with. Here, you already know how to handle the lists; the only part that you care about is "how do I divide two integers and get the result represented as a fraction?", and if you have that, then you can just plug that in to your technique for "combining" the two lists. Therefore, you should *not mention* the part about the lists and just ask (the search engine first, and then Stack Overflow) about fractions. – Karl Knechtel Aug 17 '21 at 21:58
  • As I feared, I obviously made a number of mistakes in posting my questions, as all of your comments show. Again, I apologize. In addition, there is a message above my question now (from an administrator I think?), saying that my question has already been answered in two other places, and giving me the (only) options of editing or deleting my question. I haven't acted on this, because the the answers listed by the administrator are much more complicated than the answer given by Andrej, and I'm not sure that I could have figured out how to do what I wanted from them. – Jerry Aug 17 '21 at 22:10
  • Likewise, the recommendations in the comments are certainly useful, but I don't know how long it would have taken me to figure out how to solve my problem by following them, or if I could have at all. So it seems to me that the answer Andrej provided is very useful, not just for me but for others who may have very little programming knowledge, and I would like to 'close' the question, rather than edit or delete it. But perhaps that is not what Stack Overflow is for. Please advise. – Jerry Aug 17 '21 at 22:17

1 Answers1

3

You can use fractions module:

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))

Prints:

1/2
2/3
9/8
8/15
5/3
9/14
7/6
2/3
3/2
8/11
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    Thank you! Thanks also to all who commented, and the contributor who sent me a private message (which was valuable, but much more complicated than the answer Andrej Kesely provided). – Jerry Aug 17 '21 at 21:53