Avión
Avión

Reputation: 8386

Print a python array on a specific format

I've the following two arrays:

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 contains two arrays inside it:

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]]

How can I print the arrays as to show the following solutions?

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]

The output I would like to have is to get each value to I can do other stuff with it. This means, I would like to store for the first column of the array:

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

What I've tried so far:

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

thanks in advance

Upvotes: 2

Views: 105

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250891

You can use zip here:

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

Output:

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

Update:

To assign the items of column to variables or dictionary keys then you can change the above code to:

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

Don't use type as a variable name, it's a built-in function

Upvotes: 3

Related Questions