无法理解此代码的输出:将python列表转换为1-d numpy数组 [英] Cannot understand the output of this code: python list to 1-d numpy array

查看:64
本文介绍了无法理解此代码的输出:将python列表转换为1-d numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个将python列表转换为numpy数组的方法,但是却得到了非常不直观的输出.当然我做错了,但是出于好奇,我想知道为什么得到这样的输出.这是我的代码:

I tried to create turn a python list into a numpy array, but got very unintuitive output. Certainly I did something wrong, but out of curiosity I would like to know why I got such an output. Here is my code:

import numpy as np

# Exercise 2
a = [1, 5, 3, 6, 2]
b = np.ndarray(a)
print(b, b.dtype)

输出为

[[[[[0.00000000e+000 6.93284651e-310]
    [6.93284792e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]]

   [[6.93284744e-310 2.20882835e-314]
    [6.93284743e-310 6.93284743e-310]
    [6.93284743e-310 6.93284650e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]
    [6.93284744e-310 6.93284744e-310]]

   ... (12 more blocks similar to ones above)

   [[6.93284871e-310 6.93284871e-310]
    [6.93284745e-310 6.93284871e-310]
    [6.93284651e-310 6.93284871e-310]
    [6.93284745e-310 6.93284871e-310]
    [6.93284871e-310 6.93284745e-310]
    [6.93284727e-310 6.93284871e-310]]]]] float64

推荐答案

您创建了一个五维数组.

You created a five dimensional array.

a = [1, 5, 3, 6, 2]
b = np.ndarray(a)
print(b.shape)

给予

(1, 5, 3, 6, 2)

np.ndarray 的第一个参数是数组的形状.您可能想要的

The first argument of the np.ndarray is the shape of the array. Your probably wanted

b = np.array(a)
print(b)

给出

[1 5 3 6 2]

这篇关于无法理解此代码的输出:将python列表转换为1-d numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆