Finding twin primes in a list and recording the order they appear in
<p>I am working on a program right now that has the goal of analyzing twin primes in a certain way(twin primes are primes in the form of(p,p+2)). Right now I have a code that counts the twin primes remainders in % 10 form. </p>

<p>This is that code:</p>

<pre><code>def twin_prime_counter_type10(n):
    not_prime = []
    prime = []
    A = range(n + 1)
    B = range(n + 1)
    for i in xrange(2, n+1):
        if i not in not_prime:
            prime.append(i)
            for j in xrange(i*i, n+1, i):
                not_prime.append(j)   
    for n,i in enumerate(prime):
        if not A[n] == prime[n]:
            A[i] = 1
    count1_3 = 0
    count7_9 = 0
    count9_1 = 0
    for i in B:
        if B[i] % 10 == 3 and B[i - 2] % 10 == 1:
            if A[i] * A[i-2] == 1:
                count1_3 += 1
        elif B[i] % 10 == 9 and B[i - 2] % 10 == 7:
            if A[i] * A[i-2] == 1:
                count7_9 += 1
        elif B[i] % 10 == 1 and B[i - 2] % 10 == 9:
            if A[i] * A[i-2] == 1:
                count9_1 += 1
    print count1_3
    print count7_9
    print count9_1
</code></pre>

<p>print sieve(10000)</p>

<p>This part of the code works fine but I was wondering if anyone knows of a way that when I am finding the pairs of (1)'s(the twin primes) I could also record the order they appear in the list. I don't need anyone to actually write the code that does this, I am just asking if anyone knows of a built in tool in python that I could use to preform this task.</p>

<p>Thanks for the help. </p>

<p>assuming that <code>sieve</code> is the <a href="http://stackoverflow.com/questions/3939660/sieve-of-eratosthenes-finding-primes-python">Sieve of Eratosthenes</a>, or <a href="http://stackoverflow.com/questions/2211990/how-to-implement-an-efficient-infinite-generator-of-prime-numbers-in-python">similar</a>, then you can use the <a href="https://docs.python.org/3/library/itertools.html#itertools-recipes" rel="nofollow"><code>pairwise</code></a> recipe in the <a href="https://docs.python.org/3/library/itertools.html" rel="nofollow">itertools</a> module to obtain only the twin primes with a generator expression </p>

<pre><code>from itertools import tee #, izip # &lt;-- for python 2

def pairwise(iterable):
    "s -&gt; (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b) #izip(a, b) # &lt;-- for python 2

def twin_primes(n):
    return ( (p1,p2) for p1,p2 in pairwise(sieve(n)) if p1+2 == p2 )
    # or use list comprehension, but I prefer generator as they use 
    # less memory and can be infinite, as sieve can be made to be 
    # infinite as well
</code></pre>

<p>for example</p>

<pre><code>&gt;&gt;&gt; print( list(twin_primes(100)) )
[(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73)]
&gt;&gt;&gt;
</code></pre>

<p>likewise you can use it alongside enumerate to get the index of the first prime in the pair in relation to a list of primes, for example</p>

<pre><code>&gt;&gt;&gt; primes=list(sieve(100))
&gt;&gt;&gt; primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]   
&gt;&gt;&gt; twins=[ (i,pair) for i,pair in enumerate( pairwise(primes) ) if pair[0]+2 == pair[1] ]
&gt;&gt;&gt; twins
[(1, (3, 5)), (2, (5, 7)), (4, (11, 13)), (6, (17, 19)), (9, (29, 31)), (12, (41, 43)), (16, (59, 61)), (19, (71, 73))]
&gt;&gt;&gt; primes[1]
3
&gt;&gt;&gt; primes[4]
11
&gt;&gt;&gt; primes[9]
29
&gt;&gt;&gt; 
</code></pre>

python