userrandomnums
userrandomnums

Reputation: 255

Matrix of zeroes and ones without numpy

How would I create a matrix of single zeroes and ones in a size I specify without numpy? I tried looking this up but I only found results using it. I guess it would be using loops? Unless there's a more simple method?

For example, the size I specify could be 3 and the grid would be 3x3.

      Col 0    Col 1    Col 2
Row 0   0        1        0
Row 1   0        0        1
Row 2   1        1        1

Upvotes: 1

Views: 1880

Answers (1)

sloth
sloth

Reputation: 101032

You could use a list comprehension:

def m(s):
  return [s*[0] for _ in xrange(s)]

Upvotes: 3

Related Questions