List comprehensions adding a value to an element based on a condition
<p>I have written code that returns the unique prime number factors of a given number.Here is that code. This code does not consider that 1 is a unique prime number. The <code>factorial3</code> is the function that returns a list of factorials for any given number.</p>

<pre><code> def factorials(n):
     x=[j for j in range(1, ((n/2)+1)) if n%j==0]
     x.append(n)
     return x

 n=input()
 numbers=[]
 for i in range(n):
    numbers.append(input())

 count=[0 for j in numbers]
 for i,k in enumerate(numbers):
    for j in factorial3(k):
            if factorial3(j)==[1,j]:
                    count[i]+=1
</code></pre>

<p>The list <code>count</code> gives me the unique prime numbers of numbers inputted.</p>

<p>For example, for 2 number 10, 20,the factors are [2,5,10] and [2,5, 10, 20]. However, the unique prime factors are [2,5] and [2,5] respectively. And hence, the count is [2,2]</p>

<p>I am trying to achieve the same with list comprehensions but have failed so far.This are the things that I have tried so far.</p>

<pre><code>c=[0 for i in numbers]

new=[i+1 for i,k in enumerate(numbers) for j in factorial3(k) if factorial3(j)==[1,j]]
print new
new1=[c[i]+1 for i,k in enumerate(numbers) for j in factorial3(k) if factorial3(j)==[1,j]]
print new1
</code></pre>

<p>I am almost there, just missing the one thing cuts down the iteration just once.</p>

<p>If you want the prime number factors, then simply go up to square root, rather than the whole range.</p>

<p>Replace your first line:</p>

<pre><code>c=[0 for i in numbers]
</code></pre>

<p>with:</p>

<pre><code>c=[0 for i in numbers**0.5]
</code></pre>

python