Dividing every value in one list by every value in another list
<p>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.</p>

<pre><code>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...]
</code></pre>

<p>How would I accomplish this?
Edit:
Here is my code:</p>

<pre><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 = []
</code></pre>

<p>Would I do something like this:</p>

<pre><code>for x in P_s:
for y in Q_s:
    p_zeros.append(x/y)
</code></pre>

<p>A solution without itertools, for whomever may want it:</p>

<p>To get this result:</p>

<pre><code>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...]
</code></pre>

<p><strong>English Explanation</strong></p>

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

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

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

<p><strong>Python Code</strong> (correct me if I'm wrong it's untested an hastily written)</p>

<pre><code>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)
</code></pre>

<p><strong>Postword</strong></p>

<p>You didn't mention what version of python you're using. This will influence the default behavior when dividing integers. See <a href="http://stackoverflow.com/questions/183853/in-python-what-is-the-difference-between-and-when-used-for-division">this SO Q/A</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.</p>

python math