How can I define _SOME CODE_
in the next code fragment in order to get the results shown below?
vector = numpy.array([a,b,c,d])
for i in xrange(4):
print vector[_SOME CODE_ using i]
It sould give me those results:
[a,b,c]
[a,c,d]
[a,b,d]
[b,c,d]
The order is not important.
Sven Marnach
607k123 gold badges966 silver badges865 bronze badges
asked Dec 2, 2011 at 16:24
-
What's the intended pattern? Add 1 to some vector components?Sven Marnach– Sven Marnach2011年12月02日 16:25:55 +00:00Commented Dec 2, 2011 at 16:25
-
vector doesn't contain 3, so you cannot get the desired results with indexing alone.Charles– Charles2011年12月02日 16:28:11 +00:00Commented Dec 2, 2011 at 16:28
2 Answers 2
Answer for the edited question:
>>> vector = numpy.array([0, 1, 2, 3])
>>> for i in xrange(4):
... print numpy.r_[vector[:i], vector[i+1:]]
...
[1 2 3]
[0 2 3]
[0 1 3]
[0 1 2]
Answer for the original question: Here's some random code producing the desired output:
>>> import numpy
>>> vector = numpy.array([0,1,2])
>>> for i in xrange(4):
... print vector + (vector >= i)
...
[1 2 3]
[0 2 3]
[0 1 3]
[0 1 2]
I've got no idea if this is what you want -- the requirement specification left some room for interpretation.
answered Dec 2, 2011 at 16:29
Comments
I think you're trying to find all combinations of size 3 of the vector. You can do this with itertools.combinations like this:
>>> import numpy
>>> import itertools
>>> vector = numpy.array([0, 1, 2, 3])
>>> list(itertools.combinations(vector, 3))
[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
answered Dec 2, 2011 at 17:41
Comments
lang-py