
Hello, i would like to produce lists of lists 1's and 0's. For example, to produce the list composed of: L = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]] I just need to do the following: n=4 numpy.eye(n,dtype=int).tolist() 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]] Any ideas? Thanks. -- View this message in context: http://old.nabble.com/lists-of-zeros-and-ones-tp27950978p27950978.html Sent from the Numpy-discussion mailing list archive at Nabble.com.

See itertools.permutations (python standard library) e.g. In [3]: list(itertools.permutations([1,1,0,0])) Out[3]: [(1, 1, 0, 0), (1, 1, 0, 0), (1, 0, 1, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 0, 1), (1, 1, 0, 0), (1, 1, 0, 0), (1, 0, 1, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 0, 1, 1), (0, 0, 1, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 0, 1, 1), (0, 0, 1, 1)] Hope that helps, -Joe On Fri, Mar 19, 2010 at 9:53 AM, gerardob <gberbeglia@gmail.com> wrote:
Hello, i would like to produce lists of lists 1's and 0's.
For example, to produce the list composed of:
L = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
I just need to do the following:
n=4 numpy.eye(n,dtype=int).tolist()
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]]
Any ideas? Thanks.
-- View this message in context: http://old.nabble.com/lists-of-zeros-and-ones-tp27950978p27950978.html Sent from the Numpy-discussion mailing list archive at Nabble.com.
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Fri, Mar 19, 2010 at 8:17 AM, Joe Kington <jkington@wisc.edu> wrote:
See itertools.permutations (python standard library) e.g. In [3]: list(itertools.permutations([1,1,0,0])) Out[3]: [(1, 1, 0, 0), (1, 1, 0, 0), (1, 0, 1, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 0, 1), (1, 1, 0, 0), (1, 1, 0, 0), (1, 0, 1, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 0, 1, 1), (0, 0, 1, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1),
(0, 0, 1, 1), (0, 0, 1, 1)] Hope that helps, -Joe
That treats each 1 as distinct. set() solves that:
list(set(itertools.permutations([1,1,0,0])))
[(1, 0, 1, 0), (1, 1, 0, 0), (0, 0, 1, 1), (1, 0, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1)]

On Fri, Mar 19, 2010 at 10:17 AM, Joe Kington <jkington@wisc.edu> wrote:
See itertools.permutations (python standard library)
e.g. In [3]: list(itertools.permutations([1,1,0,0])) Out[3]: [(1, 1, 0, 0), (1, 1, 0, 0), (1, 0, 1, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 0, 1), (1, 1, 0, 0), (1, 1, 0, 0), (1, 0, 1, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 0, 1, 1), (0, 0, 1, 1), (0, 1, 1, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 0, 1),
(0, 0, 1, 1), (0, 0, 1, 1)]
Hope that helps, -Joe
If you use "set" you automatically eliminate replicates: a = set(permutations([0,0,1,1])) a set([(0, 0, 1, 1), (0, 1, 0, 1), (0, 1, 1, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 1, 0, 0)]) Converting back to a list: b = list(a) -- Gökhan

On Fri, Mar 19, 2010 at 7:53 AM, gerardob <gberbeglia@gmail.com> wrote:
Hello, i would like to produce lists of lists 1's and 0's.
For example, to produce the list composed of:
L = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
I just need to do the following:
n=4 numpy.eye(n,dtype=int).tolist()
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]]
Any ideas? Thanks.
Here's the brute force way:
for i in range(4): ....: for j in range(i+1, 4): ....: x = np.zeros(4) ....: x[i] = 1 ....: x[j] = 1 ....: print x ....: ....: [ 1. 1. 0. 0.] [ 1. 0. 1. 0.] [ 1. 0. 0. 1.] [ 0. 1. 1. 0.] [ 0. 1. 0. 1.] [ 0. 0. 1. 1.]

On Fri, Mar 19, 2010 at 11:17 AM, Keith Goodman <kwgoodman@gmail.com> wrote:
On Fri, Mar 19, 2010 at 7:53 AM, gerardob <gberbeglia@gmail.com> wrote:
Hello, i would like to produce lists of lists 1's and 0's.
For example, to produce the list composed of:
L = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
I just need to do the following:
n=4 numpy.eye(n,dtype=int).tolist()
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]]
Any ideas? Thanks.
Here's the brute force way:
for i in range(4): ....: for j in range(i+1, 4): ....: x = np.zeros(4) ....: x[i] = 1 ....: x[j] = 1 ....: print x ....: ....: [ 1. 1. 0. 0.] [ 1. 0. 1. 0.] [ 1. 0. 0. 1.] [ 0. 1. 1. 0.] [ 0. 1. 0. 1.] [ 0. 0. 1. 1.]
here are two numpy version, but they don't take any shortcuts
a= np.array(list(np.ndindex(*2*np.ones(4)))) a array([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 1], [0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 0, 1, 1], [1, 1, 0, 0], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1]]) a[a.sum(1)==2] array([[0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 1, 0, 0]])
[list(r) for r in np.ndindex(*2*np.ones(4)) if sum(r)==2] [[0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 1, 0, 0]]
Josef
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Fri, Mar 19, 2010 at 11:24 AM, <josef.pktd@gmail.com> wrote:
On Fri, Mar 19, 2010 at 11:17 AM, Keith Goodman <kwgoodman@gmail.com> wrote:
On Fri, Mar 19, 2010 at 7:53 AM, gerardob <gberbeglia@gmail.com> wrote:
Hello, i would like to produce lists of lists 1's and 0's.
For example, to produce the list composed of:
L = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
I just need to do the following:
n=4 numpy.eye(n,dtype=int).tolist()
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]]
Any ideas? Thanks.
Here's the brute force way:
for i in range(4): ....: for j in range(i+1, 4): ....: x = np.zeros(4) ....: x[i] = 1 ....: x[j] = 1 ....: print x ....: ....: [ 1. 1. 0. 0.] [ 1. 0. 1. 0.] [ 1. 0. 0. 1.] [ 0. 1. 1. 0.] [ 0. 1. 0. 1.] [ 0. 0. 1. 1.]
here are two numpy version, but they don't take any shortcuts
a= np.array(list(np.ndindex(*2*np.ones(4)))) a array([[0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 1, 1], [0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 0, 1, 1], [1, 1, 0, 0], [1, 1, 0, 1], [1, 1, 1, 0], [1, 1, 1, 1]]) a[a.sum(1)==2] array([[0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 1, 0, 0]])
[list(r) for r in np.ndindex(*2*np.ones(4)) if sum(r)==2] [[0, 0, 1, 1], [0, 1, 0, 1], [0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 1, 0], [1, 1, 0, 0]]
just for fun
ndim=5;n=3;nel=2;len([list(r) for r in np.ndindex(*n*np.ones(ndim)) if (np.array(r)!=0).sum()==nel]) 40
Josef
Josef
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

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
participants (6)
-
Alan G Isaac
-
gerardob
-
Gökhan Sever
-
Joe Kington
-
josef.pktd@gmail.com
-
Keith Goodman