[Numpy-discussion] lists of zeros and ones

Alan G Isaac aisaac@american....
Fri Mar 19 11:05:47 CDT 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

list(set(itertools.permutations([1,1]+[0]*(n-2))))

(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
a.tolist()

hth,
Alan Isaac



More information about the NumPy-Discussion mailing list