[攻略][Python]给array类型的数据添加方括号、去掉方括号

在python使用中经常会用到array类型的数据,但是如何给里面的元素添加或者去掉其中的一个或两个方括号呢?

使用工具

Python.3.7
设定得到a的值为

>>a
[[113  50]]

添加方括号

如果直接给a加方括号,则得到,

>>[a]
[array([[717, 628]], dtype=int32)]

这样是错误的,应该通过array()函数,

>>import numpy as np
>>np.array([a])
array([[[717, 628]]], dtype=int32)

去掉方括号

去掉方括号就比较简单一些,

>>a[0]
array([717, 628], dtype=int32)


写于关雎


在这里插入图片描述


新浪微博:https://weibo.com/tianzhejia
CSDN博客:https://blog.csdn.net/qq_35605018
博客网站:http://www.zhijiadeboke.xyz
GitHub:https://github.com/ZhijiaTian
QQ邮箱:[email protected]
126邮箱:[email protected]
Outlook邮箱:[email protected]

以上均可与本人取得联系,欢迎探讨。^ v ^

猜你喜欢

转载自blog.csdn.net/qq_35605018/article/details/106801887
今日推荐