python矩阵转换为一维数组的实例

yipeiwu_com5年前Python基础

实例如下所示:

>>>from compiler.ast import flatten

>>>X
matrix([[ 1, 17, 13, 221, 289, 169],
 [ 1, 17, 14, 238, 289, 196],
 [ 1, 17, 15, 255, 289, 225],
 [ 1, 18, 13, 234, 324, 169],
 [ 1, 18, 14, 252, 324, 196],
 [ 1, 18, 15, 270, 324, 225],
 [ 1, 19, 13, 247, 361, 169],
 [ 1, 19, 14, 266, 361, 196],
 [ 1, 19, 15, 285, 361, 225]])
>>>x = X.tolist()
>>>x
[[ 1, 17, 13, 221, 289, 169],
 [ 1, 17, 14, 238, 289, 196],
 [ 1, 17, 15, 255, 289, 225],
 [ 1, 18, 13, 234, 324, 169],
 [ 1, 18, 14, 252, 324, 196],
 [ 1, 18, 15, 270, 324, 225],
 [ 1, 19, 13, 247, 361, 169],
 [ 1, 19, 14, 266, 361, 196],
 [ 1, 19, 15, 285, 361, 225]]
>>>xx = flatten(x)
>>>xx
[1,
 17,
 13,
 221,
 289,
 169,
 1,
 17,
 14,
 238,
 289,
 196,
 1,
 17,
 15,
 255,
 289,
 225,
 1,
 18,
 13,
 234,
 324,
 169,
 1,
 18,
 14,
 252,
 324,
 196,
 1,
 18,
 15,
 270,
 324,
 225,
 1,
 19,
 13,
 247,
 361,
 169,
 1,
 19,
 14,
 266,
 361,
 196,
 1,
 19,
 15,
 285,
 361,
 225]

以上这篇python矩阵转换为一维数组的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解Python 解压缩文件

详解Python 解压缩文件

zipfile模块及相关方法介绍: 1 压缩 1.1 创建zipfile对象 zipfile.ZipFile(file, mode='r', compression=0, allowZi...

在python中对变量判断是否为None的三种方法总结

三种主要的写法有: 第一种:if X is None; 第二种:if not X; 当X为None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组(...

Python转换时间的图文方法

Python转换时间的图文方法

time模块常用的中时间的转换。 python中的时间戳:通俗讲就是某个时刻的时间,单位是秒; 获取当前时间的时间戳: time.time() 1)没有参数, 2)返回从1970年1月1...

Python中防止sql注入的方法详解

前言 大家应该都知道现在web漏洞之首莫过于sql了,不管使用哪种语言进行web后端开发,只要使用了关系型数据库,可能都会遇到sql注入攻击问题。那么在Python web开发的过程中s...

python 获取毫秒数,计算调用时长的方法

如题:在python的函数调用中需要记录时间,下面是记录毫秒时间的方法。 import datetime import time t1 = datetime.datetime.now...