以特定格式打印python数组

时间:2014-02-04 09:47:14

标签: python arrays list

我有以下两个数组:

array = [[[0, 1], ['foo', 'moo'], ['17-03-2014', '17-03-2014'], ['17-03-2014', '17-03-2014'], ['12:02', '12:02'], ['12:07', '12:07'], [123, 123], [1, 1], [0, 0], [0.22991937398910522, 0.45983871817588806]], [[2, 3], ['bar', 'les'], ['18-03-2014', '18-03-2014'], ['21-03-2014', '21-03-2014'], ['12:03', '12:03'], ['12:03', '12:03'], [123, 123], [1, 1], [0, 0], [0.22991937398910522, 0.45983871817588806]]]
type = ['type one', 'type two']

array里面包含两个数组:

1. [[0, 1], ['foo', 'moo'], ['17-03-2014', '17-03-2014'], ['17-03-2014', '17-03-2014'], ['12:02', '12:02'], ['12:07', '12:07'], [123, 123], [1, 1], [0, 0], [0.22991937398910522, 0.45983871817588806]]
2. [[2, 3], ['bar', 'les'], ['18-03-2014', '18-03-2014'], ['21-03-2014', '21-03-2014'], ['12:03', '12:03'], ['12:03', '12:03'], [123, 123], [1, 1], [0, 0], [0.22991937398910522, 0.45983871817588806]]

如何打印阵列以显示以下解决方案?

type one
the first column of array[0]
the second column of array[0]

type two
the first column of array[1]
the second column of array[1]

我想要的输出是获取每个值,我可以用它做其他的东西。这意味着,我想存储数组的第一列:

piece='foo'
date_in=17-03-2014
date_out=17-03-2014
time_int=12:02
time_out=12:07
num=123
op=1
urg=0
worked_time=0.22991937398910522

到目前为止我尝试过:

for i in xrange(len(type)):
    print type[i]
    for m in xrange(len(array[i])):
                print '\t' + str(array[i][m])

提前致谢

1 个答案:

答案 0 :(得分:3)

您可以在此处使用zip

arrays = [[[0, 1], ['foo', 'moo'], ['17-03-2014', '17-03-2014'], ['17-03-2014', '17-03-2014'], ['12:02', '12:02'], ['12:07', '12:07'], [123, 123], [1, 1], [0, 0], [0.22991937398910522, 0.45983871817588806]], [[2, 3], ['bar', 'les'], ['18-03-2014', '18-03-2014'], ['21-03-2014', '21-03-2014'], ['12:03', '12:03'], ['12:03', '12:03'], [123, 123], [1, 1], [0, 0], [0.22991937398910522, 0.45983871817588806]]]
types = ['type one', 'type two']

for arr, typ in zip(arrays, types):
    print typ
    for row in arr:
        item1, item2 = row
        print "{} {:>20}".format(item1, item2)
    print

<强>输出:

type one
0                    1
foo                  moo
17-03-2014           17-03-2014
17-03-2014           17-03-2014
12:02                12:02
12:07                12:07
123                  123
1                    1
0                    0
0.229919373989       0.459838718176

type two
2                    3
bar                  les
18-03-2014           18-03-2014
21-03-2014           21-03-2014
12:03                12:03
12:03                12:03
123                  123
1                    1
0                    0
0.229919373989       0.459838718176

<强>更新

要将列项分配给变量或字典键,您可以将上述代码更改为:

variables = ['piece', 'date_in', 'date_out', 'time_int', 'time_out', 'num', 'op', 'urg', 'worked_time']

for arr, typ in zip(arrays, types):
    print typ
    for col in zip(*arr):
        #if you want variables here then use:
        # piece, date_in ,.... = col
        d = dict(zip(variables, col))
        #Do something with the variables or dict here
    print

不要使用type作为变量名,它是内置函数