以特定格式打印python数组
我有以下两个阵列:以特定格式打印python数组,python,arrays,list,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-
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']
数组
包含两个数组:
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])
提前感谢您可以在此处使用
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
更新:
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=foo
,date=17-03-2014
。等等。提前谢谢。@Borja请在问题正文中发布预期输出。:)好的,给我两分钟。谢谢。@Borja我已经更新了答案,也许这就是你想要的。