sizeA = 2**28
M = [0]*sizeA

The above code creates an array but, each element is 4 bytes each.

How do create a large byte array with type 'B' (eight bits unsigned)? I would like it to be as large as possible. I need it for a sieve prime number program.

The above code creates an array but, each element is 4 bytes each.

Not an array in python we call it a list.
A list in python is much more flexible than "array" as it`s called in C/C++,java.

Python has 3 types ints, longs or floats.
eight bits unsigned(can store 0-->255) so in python it`s a int.

I would like it to be as large as possible. I need it for a sieve prime number program.

In Python integers have a size only limited by your computer's memory.
If you want to force compatibility with C you can use Python module struct

Edited 4 Years Ago by snippsat: n/a

Use the builtin function bytearray()

>>> L = bytearray(10)
>>> L
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> L[3] = 'a'
>>> L
bytearray(b'\x00\x00\x00a\x00\x00\x00\x00\x00\x00')

edit: sorry, that was not exacly your question, but perhaps you can use a block of 8 consecutive bytes for your purpose.

Edited 4 Years Ago by Gribouillis: n/a

I may not understand the question but I notice that you are creating a list of 268,435,456 items. Each item in the list (which you are calling an array) is a zero now, but you can put anything into the list - a string, a number, a long-integer, a function, etc.

I suspect that the problem you are trying to solve will yield to a different solution - There is a long discussion of prime sieve generation on http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/

import array

sizeA = 2**27
M = array.array('B',[0]*sizeA) # This was just a intuitive guess that worked

The above was what I was looking for, Although I does now work as well as:

M = {0}*sizeA

The second or old way gets more items per array (or list). Which makes no sense.

Thank you all for all your help.

M = array.array('B',[0]*sizeA)

array.array() accepts an iterable, it would be much more efficient not to create the list

M = array.array('B', (0 for x in xrange(sizeA)))

Edited 4 Years Ago by Gribouillis: n/a

array.array() accepts an iterable, it would be much more efficient not to create the list

M = array.array('B', (0 for x in xrange(sizeA)))

Actually what I said is not true, the list version is faster than the iterator version. I tried with itertools.repeat(0, sizeA) also, but the list version is still faster. Sorry.

Edited 4 Years Ago by Gribouillis: n/a

I have a 3 Gigs of memory in my Dell computer. It does not matter which way I try to get a large array, I still have a gig of memory left before Python gets a MemmoryError. There should be a way to get a larger array of bytes

Actually what I said is not true, the list version is faster than the iterator version. I tried with itertools.repeat(0, sizeA) also, but the list version is still faster. Sorry.

I have been working with Python for only about 3 weeks. None of the code at that website makes sense to me. Here is my sieve program it only works up to 30 bit primes and is slow:

# Sieve prime program
import math
from time import clock, time

Mask = [1,2,4,8,16,32,64,128]
def TEST(x):
    i=x/16
    m=Mask[(x % 16)/2]
    return M[i] & m

def SET(x):
    i=x/16
    mi = (x % 16)/2    
    m=Mask[mi]
    M[i] = M[i] | m

Pw = 30                            # find 30 bit primes
print "Power = ", Pw
max = 2**Pw
max = max - 1

start = time()
startC = clock()

MemNeeded = max/4+1
M = [0]*MemNeeded
print "\n Memory = ",MemNeeded

maxsq = int(math.sqrt(max)) + 1

for teste in range(3,maxsq,2):
    if (TEST(teste) == 0):
        mom = teste * teste
        t2 = teste * 2
        while (mom <= max):
            SET(mom)
            mom = mom + t2

print " elapsed clock:"
print (clock() - startC)

cnt = 1
n=1
while (n <= max):
    n += 2
    if (TEST(n) == 0):        
        cnt = cnt + 1
        p = n  # get last prime

print " elapsed clock:"
print (clock() - startC)
print "cnt = ",cnt,p

I may not understand the question but I notice that you are creating a list of 268,435,456 items. Each item in the list (which you are calling an array) is a zero now, but you can put anything into the list - a string, a number, a long-integer, a function, etc.

I suspect that the problem you are trying to solve will yield to a different solution - There is a long discussion of prime sieve generation on http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/

This question has already been answered. Start a new discussion instead.