美文网首页
Numpy-03 结构数组

Numpy-03 结构数组

作者: 罗泽坤 | 来源:发表于2020-03-26 20:47 被阅读0次
#  自定义结构数组
import numpy as np
persontype = np.dtype({'names':['name', 'age', 'chinese', 'math', 'english']#自定义命名空间, 
                            ,'formats':['U32','i', 'i', 'i', 'f']})
peoples = np.array([("ZhangFei",32,75,100, 90),("GuanYu",24,85,96,88.5), 
                        ("ZhaoYun",28,85,92,96.5),("HuangZhong",29,65,85,100)], dtype=persontype)
ages = peoples['age']
chineses = peoples['chinese']
maths = peoples['math']
englishs = peoples['english']
print(np.mean(ages))
print(np.mean(chineses))
print(np.mean(maths))
print(np.mean(englishs))
print(peoples['name'])
print(ages)
print(chineses)
print(maths)
print(englishs)
print(peoples)
print(peoples[0][1:])#不支持 此种切片索引因为数组是peoples是一维的
28.25
77.5
93.25
93.75
['ZhangFei' 'GuanYu' 'ZhaoYun' 'HuangZhong']
[32 24 28 29]
[75 85 85 65]
[100  96  92  85]
[ 90.   88.5  96.5 100. ]
[('ZhangFei', 32, 75, 100,  90. ) ('GuanYu', 24, 85,  96,  88.5)
 ('ZhaoYun', 28, 85,  92,  96.5) ('HuangZhong', 29, 65,  85, 100. )]



---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-98-30558e3ef830> in <module>
     19 print(englishs)
     20 print(peoples)
---> 21 print(peoples[0][1:])#不支持 此种切片索引因为数组是peoples是一维的


IndexError: too many indices for array
Meanlist = []
for i  in range(peoples.size):
    a = np.mean(list(peoples[i])[1:])
    Meanlist.append((peoples[i][0],a))
print(Meanlist)
print(sorted(Meanlist,key=lambda x:x[1]))
[(b'ZhangFei', 74.25), (b'GuanYu', 73.375), (b'ZhaoYun', 75.375), (b'HuangZhong', 69.75)]
[(b'HuangZhong', 69.75), (b'GuanYu', 73.375), (b'ZhangFei', 74.25), (b'ZhaoYun', 75.375)]
#自定义数据类型
#下面的代码格式不对
dt = np.dtype([[s32,i,i,i,f]])
hero = np.array([["ZhangFei",32,75,100, 90],["GuanYu",24,85,96,88.5], 
                     ["ZhaoYun",28,85,92,96.5],["HuangZhong",29,65,85,100]],dtype=dt)
print(hero)
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-55-7dd6262c0288> in <module>
      1 #自定义数据类型
----> 2 dt = np.dtype([[s32,i,i,i,f]])
      3 hero = np.array([["ZhangFei",32,75,100, 90],["GuanYu",24,85,96,88.5], 
      4                      ["ZhaoYun",28,85,92,96.5],["HuangZhong",29,65,85,100]],dtype=dt)
      5 print(hero)


NameError: name 's32' is not defined
print(Meanlist)
Meanlist.sort(key = lambda x:x[1],reverse=True)
print(Meanlist)
[(b'HuangZhong', 69.75), (b'GuanYu', 73.375), (b'ZhangFei', 74.25), (b'ZhaoYun', 75.375)]
[(b'ZhaoYun', 75.375), (b'ZhangFei', 74.25), (b'GuanYu', 73.375), (b'HuangZhong', 69.75)]
print(peoples)
[(b'ZhangFei', 32, 75, 100,  90. ) (b'GuanYu', 24, 85,  96,  88.5)
 (b'ZhaoYun', 28, 85,  92,  96.5) (b'HuangZhong', 29, 65,  85, 100. )]
print(peoples['math'].argmax())
print('数学最高分是',peoples[peoples['math'].argmax()][0],peoples['math'].max(),'分')
0
数学最高分是 ZhangFei 100 分

github代码地址

https://github.com/luozekun1230/MyPyhonProgram/tree/master/Python%E5%9F%BA%E7%A1%80

相关文章

  • Numpy-03 结构数组

    github代码地址 https://github.com/luozekun1230/MyPyhonProgram...

  • js 数组与树形结构对象相互转换

    数组 树形结构对象 数组转成树形结构 树形结构转成数组

  • c语言结构体类型的多维数组的读取

    定义一个结构体 定义一个结构体数组 为结构体数组赋值 定义一个函数为结构体数组中的数组赋值

  • golang入门到放弃:4.结构体

    定义 eg: 结构体数组 结构体数组 定义 结构体数组名 [元素个数]结构体类型 eg//var arr [5]P...

  • 结构体数组的定义

    结构体数组的定义 1、先定义结构体类型,再定义结构体数组 2、定义结构体类型的同时定义结构体数组 3、省略结构体类...

  • MATLAB的Structure数组

    4 Structure数组 Structure数组也称结构数组,另外还有些书籍称作架构数组。结构是MATLAB提供...

  • C语言 13 结构体数组

    C语言 13 结构体数组 结构体数组的定义 结构体数组初始化 结构体成员的使用 格式 : 字符串成员的处理 结构体...

  • 数据结构:数组

    00数据结构与算法分析:大纲01数据结构:数组02数据结构:链表03数据结构:栈03数据结构:队列 数组 数组是一...

  • 数组结构-数组

    数组下标为什么从 0 开始? 0 开始的寻址 a[k]_address = base_address + k * ...

  • 结构体与数组的关系

    结构体与数组的关系 结构体是数组的成员 一个数组的全部元素是结构体变量

网友评论

      本文标题:Numpy-03 结构数组

      本文链接:https://www.haomeiwen.com/subject/xtshuhtx.html