7

Note: I found the answer and answered my own question, but I have to wait 2 days to accept my answer.


How do I initialize a numpy array of size 800 by 800 with other values besides zero? :

array = numpy.zeros((800, 800))

I am looking for a solution like this, where I could pass in a list (or lists) of values.

 array = numpy.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

Edit: I do not want to fill it with identical values.

I wanted a quick code example that demonstrates how this works without a long detailed example like I find in the other questions. I also want something simple to understand like converting a python array into a numpy array. I also am looking for the specific case of a 2D array.

Rock Lee
  • 9,146
  • 10
  • 55
  • 88
  • 1
    I'm not sure I got the question. Do you want to build an array filled with `1`s, or filled with any value you input? Also, why you wrote the code of an array 30x30 filled with 1? – rafaelc May 23 '15 at 22:15
  • I found the answer to my question. I think it is useful since I couldn't find such a simple answer anywhere on the other answers I checked. Also, I do not want to fill with identical values. I'd like to add my answer to help others. So I would like if it was not marked as duplicate. – Rock Lee May 23 '15 at 22:35
  • Have you read through the documentation? – wwii May 23 '15 at 22:48
  • @wwii No, it is so huge and vast, and hard to find a simple example anywhere. – Rock Lee May 23 '15 at 23:11
  • [http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html#array-creation-routines](http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html#array-creation-routines) – wwii May 23 '15 at 23:51

2 Answers2

11

You can use fill method to init the array.

x = np.empty(shape=(800,800))
x.fill(1)
Wang Hui
  • 111
  • 1
  • 4
1

Found out the answer myself: This code does what I want, and shows that I can put a python array ("a") and have it turn into a numpy array. For my code that draws it to a window, it drew it upside down, which is why I added the last line of code.

# generate grid
    a = [ ]
    allZeroes = []
    allOnes = []

    for i in range(0,800):
        allZeroes.append(0)
        allOnes.append(1)

    # append 400 rows of 800 zeroes per row.
    for i in range(0, 400):
        a.append(allZeroes)

    # append 400 rows of 800 ones per row.
    for i in range(0,400):
        a.append(allOnes)


#So this is a 2D 800 x 800 array of zeros on the top half, ones on the bottom half.
array = numpy.array(a)

# Need to flip the array so my other code that draws 
# this array will draw it right-side up
array = numpy.flipud(array)
Rock Lee
  • 9,146
  • 10
  • 55
  • 88
  • better not do that if you want to modify the array afterwards since you are appending the same instance of allZeroes and allOnes 400 times each. If you modify the array after, each modification will appear to happen 400 times. Really, what you have is useless – ReubenBeeler Jul 28 '23 at 07:16