Python printing "<built-in method ... object" instead of list

12,583

You have two problems.

The first problem is that you are not actually calling the reverse method on your array arr.

You have this: arr.reverse

You have to actually call it -> arr.reverse()

Simple example below:

>>> [1,2,3].reverse
<built-in method reverse of list object at 0x100662c68>

Without calling reverse, the output you get is the uncalled reverse method of the list object. Which is very similar to the output you were getting.

The second problem you have is that reverse() method does the reverse in place, which means it performs the reverse on arr (your arr will be reversed) and returns None. So, when you are passing this:

np.array(arr.reverse())

You are returning the return of arr.reverse() to your np.array call, which is None.

So, fixing those two items, by calling arr.reverse() on its on, and then passing arr, will give you the result you are expecting:

import numpy as np
arr = list(map(float,input().split()))
arr.reverse()
res = np.array(arr)
print(res)

Demo:

1 2 3 4
[ 4.  3.  2.  1.]
Share:
12,583
Char
Author by

Char

Updated on June 05, 2022

Comments

  • Char
    Char about 2 years
    import numpy as np
    arr = list(map(float,input().split()))
    print(np.array(arr.reverse))
    

    Why is this printing this instead of the contents of the list?

    # outputs "<built-in method reverse of list object at 0x107eeeec8>"