Python打印“<内置方法...对象"而不是列表 [英] Python printing "<built-in method ... object" instead of list
问题描述
将 numpy 导入为 nparr = list(map(float,input().split()))打印(np.array(arr.reverse))
为什么要打印这个而不是列表的内容?
# 输出<0x107eeeec8 处的列表对象的内置方法反转>"
您有两个问题.
第一个问题是你实际上没有调用数组arr
的reverse方法.
你有这个:arr.reverse
你必须实际调用它 -> arr.reverse()
以下简单示例:
<预><代码>>>>[1,2,3].反向<0x100662c68处列表对象的内置方法reverse>不调用reverse
,您得到的输出是list
对象的未调用的reverse 方法.这与您获得的输出非常相似.
您遇到的第二个问题是 reverse()
方法在原地执行反向操作,这意味着它对 arr
执行反向操作(您的 arr 将被反向)并且返回 None
.所以,当你通过这个:
np.array(arr.reverse())
您正在将 arr.reverse()
的返回返回到您的 np.array
调用,即 None
.
所以,修复这两个项目,通过调用 arr.reverse()
在其上,然后 then 传递 arr
,会给你你期待的结果:
将 numpy 导入为 nparr = list(map(float,input().split()))arr.reverse()res = np.array(arr)打印(资源)
演示:
1 2 3 4[ 4. 3. 2. 1. ]
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>"
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.]
这篇关于Python打印“<内置方法...对象"而不是列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!