python 中使用zip实现矩阵转置

 

001、

复制代码
[root@PC1 test04]# ls
a.txt  test.py
[root@PC1 test04]# cat a.txt        ## 测试数据
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
[root@PC1 test04]# cat test.py      ## 测试程序
#!/usr/bin/env python
# -*- coding: utf-8 -*-
in_file = open("a.txt", "r")
list1 = list()
for i in in_file:
        i = i.strip().split()
        list1.append(i)
for i in list(zip(*list1)):
        i = [str(j) for j in i]
        i = " ".join(i)
        print(i)
[root@PC1 test04]# python3 test.py    ## 转置结果
01 11 21
02 12 22
03 13 23
04 14 24
05 15 25
06 16 26
07 17 27
08 18 28
09 19 29
10 20 30
复制代码

 

posted @   小鲨鱼2018  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示