2

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
2
  • What's the intended pattern? Add 1 to some vector components? Commented Dec 2, 2011 at 16:25
  • vector doesn't contain 3, so you cannot get the desired results with indexing alone. Commented Dec 2, 2011 at 16:28

2 Answers 2

3

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

0

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.