0

I have an array like this:

array = [([476301.98163511883, 6176897.129456658],)
     ([476723.365551495, 6176895.078376785],)
     ([477124.59457628336, 6176893.28525448],)
     ([477525.82249430567, 6176891.306532074],)
     ([477927.0510582989, 6176889.4760845825],)
     ([477925.0121537624, 6176487.379134962],)
     ([477922.97333802003, 6176085.2824224755])]

All I'm trying to do is add 5 to the first number and 10 to the second. I tried this

for x in numpy.nditer(array, op_flags=['readwrite']):
    x + numpy.array([5, 10])

Which gives TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray'

Yet something like this works fine

b = numpy.array([0, 10])
x = numpy.array([10, 10])
c = x + b
2
  • What's with the extra parens? Commented Dec 3, 2014 at 4:37
  • @Aaron Hall It's how my data comes in. I don't really have any control over it. Guessing this is the root of my problem. Commented Dec 3, 2014 at 4:38

1 Answer 1

1

After adjusting your data by the removing the extra parentheses (which were missing commas, and so would not be legal Python) so that it's a 7 by 2 array, I then add it to an array of 5, 10:

import numpy as np
array = np.array([[476301.98163511883, 6176897.129456658],
     [476723.365551495, 6176895.078376785],
     [477124.59457628336, 6176893.28525448],
     [477525.82249430567, 6176891.306532074],
     [477927.0510582989, 6176889.4760845825],
     [477925.0121537624, 6176487.379134962],
     [477922.97333802003, 6176085.2824224755]])

and now:

>>> array
array([[  476301.98163512,  6176897.12945666],
       [  476723.36555149,  6176895.07837678],
       [  477124.59457628,  6176893.28525448],
       [  477525.82249431,  6176891.30653207],
       [  477927.0510583 ,  6176889.47608458],
       [  477925.01215376,  6176487.37913496],
       [  477922.97333802,  6176085.28242248]])
>>> array + np.array([5, 10])
array([[  476306.98163512,  6176907.12945666],
       [  476728.36555149,  6176905.07837678],
       [  477129.59457628,  6176903.28525448],
       [  477530.82249431,  6176901.30653207],
       [  477932.0510583 ,  6176899.47608458],
       [  477930.01215376,  6176497.37913496],
       [  477927.97333802,  6176095.28242248]])

Array addition will be much faster than iteratively adding the elements together.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. How do I adjusting the data so that it's a 7 by 2 array? The extra parens are causing problems.
Delete them, that's what I did. It's not legal python.
It's how my data comes in. I have limited control on the output, I can make it look like this: [(476301.98163511883, 6176897.129456658) (476723.365551495, 6176895.078376785) (477124.59457628336, 6176893.28525448)] but I still run into the same problem
put commas between the parentheses groups, and then you can pass it to np.array.

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.