0

If I was writing C++ I could do the following:

double foo [9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; 
double* bar = foo;
cout << bar[0] << ", " << bar[1] << ", " << bar[2] << "\n";
bar += 3;
cout << bar[0] << ", " << bar[1] << ", " << bar[2] << "\n";
bar += 3;
cout << bar[0] << ", " << bar[1] << ", " << bar[2] << "\n";

This would print:

0, 1, 2
3, 4, 5
6, 7, 8

I now want to do the same in numpy:

foo = np.arange((9))
bar = foo[:3]
print(bar)
???
print(bar)
???
print(bar)

But I don't know what to put at the ??? The result would hopefully be:

[0 1 2]
[3 4 5]
[6 7 8]

In other words, I'm looking for a way to change where the view bar is pointing at foo, using the existing reference bar.

That is I would like to be able to write the ??? as bar = f(bar), where f is some function defined by the user.


Edit:

This is how I'm doing it currently:

def f(idx=[0]): #mutable default argument
    idx[0] += 3
    return foo[idx[0]-3:idx[0]]
for key in key_list:
    a_dict[key] = f()
    b_dict[key] = f()

for view in range(num_views):
    a_list.append(f())
    b_list.append(f())

It needs to be in a vector because scipy.optimize.least_squares needs all values stored in the same vector.

10
  • The proper or the hack way? Commented Dec 20, 2018 at 19:12
  • I can't think of either, but would like to know about both. Commented Dec 20, 2018 at 19:14
  • 2
    Why do you want to do this? Python and C++ are very different languages. You should learn to use Python on it's own terms, and not to use things like pointer arithmetic, which python doesn't really support (Python doesn't even have pointers). Commented Dec 20, 2018 at 19:15
  • To make my code easier to maintain and read. In my specific case I need to add many different vectors into a vector, and split them out again, and this would make it very easy to read. Commented Dec 20, 2018 at 19:22
  • @Atnas if you actually show us what you are trying to do we could probably tell you the numpy-friendly way to do it. This question is a classic case of the X-Y problem. So, instead of asking "how do you do pointer arithmetic in Numpy/Python", before people go down some hacky road of "well, maybe if you import ctypes ..." or "well, using Cython..." you could just tell us what you are trying to achieve Commented Dec 20, 2018 at 20:19

3 Answers 3

1

A pythonic way is to use indeces:

foo = np.arange(9)
bar = np.arange(3)
print(foo[bar])
>> array([0, 1, 2])
bar += 3
print(foo[bar])
>> array([3, 4, 5])
bar += 3
print(foo[bar])
>> array([6, 7, 8])

A less pythonic way (but more similar to what you are coding) is to use an iterator:

foo = np.arange(9)
bar = foo.reshape(3,3).__iter__()   # or use bar = iter(foo.reshape(3,3))
bar.__next__()   # or use next(bar)
>> array([0, 1, 2])
bar.__next__()
>> array([3, 4, 5])
bar.__next__()
>> array([6, 7, 8])
Sign up to request clarification or add additional context in comments.

9 Comments

Isn't it a bit more pythonic to use the iter() and next() functions?
@Atnas yes, it is. Or to just use a for-loop and not call either of those functions explicitly.
Also, iterators are extremely pythonic.
so, just for sub in foo.reshape(3, -1): print(sub)
@juanpa.arrivillaga, OP did not ask for a for loop, but for an explicit iterator object.
|
0
foo = np.arange(9)

bar = foo[:3]
print(bar)

bar = foo[3:6]
print(bar)

bar = foo[6:]
print(bar)

1 Comment

this is the right way to do this, but this isn't what the OP is asking for.
0
foo = np.arange((9))
loopcounter = 3
startindex = 0
stopindex = startindex + loopcounter
print(foo[startindex:stopindex])
startindex = stopindex
stopindex = startindex + loopcounter
print(foo[startindex:stopindex])
startindex = stopindex
stopindex = startindex + loopcounter
print(foo[startindex:stopindex])

...

Comments

Your Answer

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.