2

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Is there any way to print the values in vertical instead of that list which is difficult to me to manage? I would like to save each value to to other stuff. Like type=foo, date=17-03-2014. etc. Thanks in advance.
@Borja Please post the expected output in question body. :)
Okay, give me 2 minutes. Thank you.
@Borja I've updated the answer, may be that's what you wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.