0

I am using Python for the first time in a long time and am slightly lost. I have a numPy array that looks like this when I print it..

   [[148 362]
     [153 403]
     [163 443]
     [172 483]
     [186 521]
     [210 553]
     [239 581]
     [273 604]
     [314 611]
     [353 602]]

I am trying to get the 5 item from the array and save it as 2 variables, x and y.

I have tried accessing it using...

print("Item 5" + numpy_array[5])

But that is giving me an error

typeError: ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')
0

2 Answers 2

1

Suppose your array is stored in variable called numpy_array, just do. Since your subarrays contains 2 elements, it will unpack the values into x and y

x, y = numpy_array[5]
print (x, y)
# (210, 553)
Sign up to request clarification or add additional context in comments.

Comments

0

These are other examples:

print("Item 5: " + str(numpy_array[5]) ) #=> Item 5: [210 553]

print("Item 5: ", numpy_array[5][0], numpy_array[5][1] ) #=> Item 5:  210 553

print("Item 5: ", numpy_array[5][0], "-" , numpy_array[5][1] ) #=> Item 5:  210 553

print (f"Item 5: {numpy_array[5][0]}, {numpy_array[5][1]}" ) #=> Item 5: 210, 553

1 Comment

Thanks for those, lots of different options,

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.