python行列转换脚本编写

python行列转换脚本编写

一、

table=[(‘Person’,’Disks’,’Books’),
(‘Zoe’,’12’,’24’),
(‘John’,’17’,’5’),
(‘Jul’,’3’,’11’)]

命令:

print zip(*table)

输出:

[(‘Person’,’Zoe’,’John’,’Jul’),
(‘Disks’,’12’,’17’,’3’),
(‘Books’,’24’,’5’,’11’)]

二、

现在有两行数据如下:

python]# cat lianxi.bac
a b c d e f g h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
a b c d e f g h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

要求前8列不变,后面每两列为一组。行转为列。
eg.

a b c d e f g h 1 2
a b c d e f g h 3 4
···
a b c d e f g h 39 40

脚本如下:

#!/usr/bin/env Python
# -- conding:utf-8 --
# file:lianxi.py
#
import sys
import re

path=’/lianxi/python/lianxi.bac’
file=open(path,’r’)

ou=[]
for i in range(8,47):
if i%2 == 0:
ou.append(i)

for line in file:
for n in ou:
item=line.split()[0:8]
a=line.split()[n:n+2]
item += a
# print (item)
print ’ ‘.join(item)

执行结果:

python]# python 4.py
a b c d e f g h 1 2
a b c d e f g h 3 4
a b c d e f g h 5 6
a b c d e f g h 7 8
a b c d e f g h 9 10
a b c d e f g h 11 12
a b c d e f g h 13 14
a b c d e f g h 15 16
a b c d e f g h 17 18
a b c d e f g h 19 20
a b c d e f g h 21 22
a b c d e f g h 23 24
a b c d e f g h 25 26
a b c d e f g h 27 28
a b c d e f g h 29 30
a b c d e f g h 31 32
a b c d e f g h 33 34
a b c d e f g h 35 36
a b c d e f g h 37 38
a b c d e f g h 39 40
a b c d e f g h 1 2
a b c d e f g h 3 4
a b c d e f g h 5 6
a b c d e f g h 7 8
a b c d e f g h 9 10
a b c d e f g h 11 12
a b c d e f g h 13 14
a b c d e f g h 15 16
a b c d e f g h 17 18
a b c d e f g h 19 20
a b c d e f g h 21 22
a b c d e f g h 23 24
a b c d e f g h 25 26
a b c d e f g h 27 28
a b c d e f g h 29 30
a b c d e f g h 31 32
a b c d e f g h 33 34
a b c d e f g h 35 36
a b c d e f g h 37 38
a b c d e f g h 39 40

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值