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 repath=’/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