-1

I am writing a function to find all the rational zeros of a polynomial and in need to divide each number in a list by each number in a list Ex.

list1 = [1,2,3,4,5,6,10,12,15,20,30,60]
list2 = [1,2,3,6]

zeros = [1/1,2/1,3/1,4/1,...,1/2,2/2,3/2,4/2,...1/3,2/3,3/3,4/3,etc...]

How would I accomplish this? Edit: Here is my code:

from fractions import Fraction

def factor(x):
    x = abs(x)
    factors = []
    new_factors = []
    for n in range(1,x):
        if x % n == 0:
            factors.append(n)
            new_factors.append(n)
    for y in factors:
        new_factors.append(y * -1)
    new_factors = sorted(new_factors)
    return new_factors
print(factor(8))


def find_zeros(fnctn,pwr):
    last = fnctn[len(fnctn)]
    first = fnctn[0]
    P_s = factor(last)
    Q_s = factor(first)
    p_zeros = []

Would I do something like this:

for x in P_s:
for y in Q_s:
    p_zeros.append(x/y)
lilsnyderio
  • 53
  • 2
  • 6

3 Answers3

0
zeros = []
for i in list1:
    zeros.extend([i/j for j in list2])
afsafzal
  • 592
  • 5
  • 15
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Code-only answers are discouraged. – Ajean Sep 21 '16 at 16:40
0

All I needed were nested for loops

list1 = [1,2,3,4,5,6,10,12,15,20,30,60] 
list2 = [1,2,3,6]
zeros = []
for x in list1:
    for y in list2:
        zeros.append(x/y)
print(zeros)

output: [1.0, 0.5, 0.3333333333333333, 0.16666666666666666, 2.0, 1.0, 0.6666666666666666, 0.3333333333333333, 3.0, 1.5, 1.0, 0.5, 4.0, 2.0, 1.3333333333333333, 0.6666666666666666, 5.0, 2.5, 1.6666666666666667, 0.8333333333333334, 6.0, 3.0, 2.0, 1.0, 10.0, 5.0, 3.3333333333333335, 1.6666666666666667, 12.0, 6.0, 4.0, 2.0, 15.0, 7.5, 5.0, 2.5, 20.0, 10.0, 6.666666666666667, 3.3333333333333335, 30.0, 15.0, 10.0, 5.0, 60.0, 30.0, 20.0, 10.0] I appreciate your help everyone!

lilsnyderio
  • 53
  • 2
  • 6
  • In your question, you ask for `y/x`, here you put `x/y`. Also your `floats` are imprecise of course and this is less elegant than the `itertools.product` solution, although I appreciate it may be easier to understand :) – Chris_Rands Sep 21 '16 at 15:42
0

A solution without itertools, for whomever may want it:

To get this result:

list1 = [1,2,3,4,5,6,10,12,15,20,30,60]
list2 = [1,2,3,6]

zeros = [1/1,2/1,3/1,4/1,...,1/2,2/2,3/2,4/2,...1/3,2/3,3/3,4/3,etc...]

English Explanation

You can iterate a variable, say i, through range(len(list1)) to get the index of each element in list1 in order.

To get the corresponding index in list2 just floor divide i by len(list1)/float(len(list2)).

Then we'll take the element from list1 and divide by the element in list2 and add that to some initially empty list (you named this zeros).

Python Code (correct me if I'm wrong it's untested an hastily written)

zeros = []
list1 = [1,2,3,4,5,6,10,12,15,20,30,60]
list2 = [1,2,3,6]
for i in range(len(list1)):
    zeros.append(list1[i] / float(list2[i//(len(list1)/float(len(list2))))])
print(zeros)

Postword

You didn't mention what version of python you're using. This will influence the default behavior when dividing integers. See this SO Q/A for an explanation of division for the major versions of python. I expect you want float division because 1/4 with evaluate to zero with floor division. The code I wrote should work for either version of python, as 1//2 is explicit floor division and 1/float(2) is explicit float division.

Community
  • 1
  • 1
Samie Bencherif
  • 1,285
  • 12
  • 27