为什么我不能创建这样的numpy数组:array([1,2],3) [英] Why can't I create a numpy array like this: array([1, 2], 3)

查看:123
本文介绍了为什么我不能创建这样的numpy数组:array([1,2],3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from numpy import array
test_list = [[1,2],3]
x = array(test_list) #ValueError: setting an array element with a sequence.

基本上,我有一个具有2个坐标和一个比例的点,并且我试图将几个放置在ndarray上,但现在不能这样做.我可以只使用[1,2,3],但我很好奇为什么不能将这种类型的列表存储在数组中.

Basically, I have a point with 2 coordinates and a scale and I was trying to put several on a ndarray but I can't do it right now. I could just use [1,2,3] but I'm curious about why I can't store that type of list in an array.

推荐答案

失败,因为数组不是矩形.如果我们将3更改为[3, 4],那么它将起作用.

It's failing because the array is non-rectangular. If we change the 3 to [3, 4] then it works.

>>> array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

这篇关于为什么我不能创建这样的numpy数组:array([1,2],3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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