Charles Charles - 1 year ago 165
Python Question

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

import numpy as np
arr = list(map(float,input().split()))

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

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

Answer Source

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:


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()))
res = np.array(arr)


1 2 3 4
[ 4.  3.  2.  1.]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download