Ai
0 Star 1 Fork 0

xmhexi/learn-python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ListTransform.py 3.86 KB
一键复制 编辑 原始数据 按行查看 历史
xmhexi 提交于 2018-12-24 11:33 +08:00 . 使用三种方式对List矩阵进行变换
#!/usr/bin/env python3
#coding:utf-8
__author__ = 'xmxoxo'
import numpy
lstDat = [
[1,2],
[3,4],
]
print ('原始数据:')
for x in lstDat :
print (x)
#---------------------------------------
## 使用列表生成式
def listGen (lstDat):
#transpose
print('-'* 30)
print ('转置矩阵:')
tp = [[row[i] for row in lstDat ] for i in range(len(lstDat[0]))]
for item in tp: print (item)
print('-'* 30)
#逆时针转90度
print ('逆时针转90度:')
rt90 = [[row[i] for row in lstDat ] for i in range(len(lstDat[0])-1,-1,-1) ]
for item in rt90: print (item)
print('-'* 30)
#顺时针转90度
print ('顺时针转90度:')
rt90 = [[lstDat[j][i] for j in range(len(lstDat)-1,-1,-1) ] for i in range(len(lstDat[0]))]
for item in rt90: print (item)
print('-'* 30)
print ('左右翻转:')
flip = [[row[i] for i in range(len(row)-1,-1,-1) ] for row in lstDat]
for item in flip: print (item)
#-------------------------
## 使用Map
def mapzip (lstDat):
print('-'* 30)
print ('转置矩阵:')
matrix = lstDat.copy()
matrix[:] = map(list,zip(*matrix))
for item in matrix: print (item)
#顺时针转90度
print ('顺时针转90度:')
matrix = lstDat.copy()
matrix[:] = map(list,zip(*matrix[::-1]))
for item in matrix: print (item)
#逆时针转90度
print ('逆时针转90度:')
matrix = lstDat.copy()
matrix[:] = map(list,zip(*matrix))
matrix[:] = matrix[::-1]
for item in matrix: print (item)
#左右翻转
print ('左右翻转:')
matrix = lstDat.copy()
matrix[:] = map(list,zip(*matrix))
matrix[:] = matrix[::-1]
matrix[:] = map(list,zip(*matrix))
for item in matrix: print (item)
def rotate(matrix):
''' """
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
'''
matrix[:] = map(list,zip(*matrix[::-1]))
return matrix
#--------------------------
## 使用numpy
def usenumpy (lstDat):
nd = numpy.array(lstDat)
#逆时针转90度:
print ('逆时针转90度:')
nd1 = numpy.rot90(nd)
print (nd1)
print ('顺时针转90度:')
nd1 = numpy.rot90(nd,k = -1)
print (nd1)
print ('左右翻转:')
nd1 = numpy.rot90(nd)
nd1 = numpy.transpose(nd1)
print (nd1)
## 输出所有状态
def getstatus (lstDat):
nd = numpy.array(lstDat)
index = 0
for i in range (4) :
index +=1
print('-'* 10 + str(index) + '-' * 10)
print (nd)
#使用列表生成式可转出为字符串
#print (','.join([str(y) for x in nd.tolist() for y in x]))
index +=1
print('-'* 10 + str(index) + '-' * 10)
nd2 = numpy.rot90(nd) #,k = -1,axes=(1, 0))
nd2 = numpy.transpose(nd2)
print (nd2)
#使用列表生成式可转出为字符串
#print (','.join([str(y) for x in nd2.tolist() for y in x]))
nd = numpy.rot90(nd)
if __name__ == '__main__':
pass
# 使用列表生成器
print ('使用列表生成器:')
listGen (lstDat)
print('='* 30)
# 使用Map
print ('使用Map+Zip:')
mapzip (lstDat)
print('='* 30)
# 使用numpy
print ('使用numpy:')
usenumpy(lstDat)
print('='* 30)
# 输出所有状态(8种)
print ('输出所有状态:')
getstatus (lstDat)
"""
三种思路的对比:
1.列表生成器。感觉这个比较传统,还是老思路,一个个去循环。作为练手可以用用。
2. 使用map 具有py的新思维,语句也比较简洁,建议使用。
3. 使用numpy。 如果是大规模的数据,涉及到大量计算,比如ML,DL之类,还是直接用库吧。
"""
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/xmhexi/learn-python.git
git@gitee.com:xmhexi/learn-python.git
xmhexi
learn-python
learn-python
master

搜索帮助