[Numpy-discussion] lists of zeros and ones

Alan G Isaac aisaac at american.edu
Fri Mar 19 12:05:47 EDT 2010

On 3/19/2010 10:53 AM, gerardob wrote:
> I would like to know a simple way to generate a list containing all the
> lists having two 1's at each element.
> Example, n = 4
> L2 = [[1,1,0,0],[1,0,1,0],[1,0,0,1],[0,1,1,0],[0,1,0,1],[0,0,1,1]]

I like


(Modify if you don't like the tuples.)
But you can use a NumPy array if you wish:

cols = list(itertools.combinations(range(n),2))
r = len(cols)
rows = np.arange(r)[:,None]
a = np.zeros((r,n),dtype=np.int_)
a[rows,cols] = 1

Alan Isaac

More information about the NumPy-Discussion mailing list