0

I have the following lists:

#Note: Both lists only contain numbers, for the sake of clearity in the example there is one with letters
a = [1,2,3,4,5,6]
b = [a,b,c,d,,e,f] 

What I need:

example = 
   1   2   3   4   5   6
a  1,a 2,a 3,a 4,a 5,a 6,a 
b  1,b 2,b 3,b 4,b 5,b 6,b
c  1,c 2,c 3,c 4,c 5,c 6,c
d  1,d 2,d 3,d 4,d 5,d 6,d
e  1,e 2,e 3,e 4,e 5,e 5,e

I must be able to access every element of the table since I will use its values (i.e. 4 AND d) as values for another operation.

Since I do not know how this "table" is called, I am having a hard time looking into documentations... Any idea what I am looking for?

four-eyes
  • 10,740
  • 29
  • 111
  • 220
  • `a = [[val1,val2,val3,...],[val4,val5,val6,...],...]` and you can access, say, val3 by doing `a[0][2]`. – Depado Mar 10 '15 at 13:26
  • @Depado that could be awkward if the lists aren't consecutive numbers. I'd be inclined to use a dictionary instead so you could access values in a more intuitive way. – Holloway Mar 10 '15 at 13:28
  • I edited my comment. Because in fact, accessing an item in a list of list is already using consecutive numbers to do so. I really don't see where the problem is, actually xD – Depado Mar 10 '15 at 13:30

1 Answers1

4

Try itertools.product(). It will return a "flattened" version of your table. Since it generates the elements one at a time instead of all at once, it's a good choice for an intermediate element of some long-running algorithm.

Here is an example.

Community
  • 1
  • 1
Kevin
  • 28,963
  • 9
  • 62
  • 81
  • above is right. Here is a one with an [example][1] [1]: http://stackoverflow.com/a/533917 – Saikiran Yerram Mar 10 '15 at 13:30
  • Will it keep its order? The two lists are created in a certain order and need to be written exactly the way I wrote it. – four-eyes Mar 10 '15 at 13:32
  • @Stophface: The order will be `(1, a), (1, b), (1, c), ... (2, a), (2, b), (2, c), ...` if you put the numeric list as the first argument. If you put the alphabetic list first, you'll get `(a, 1), (a, 2), (a, 3), ... (b, 1), (b, 2), (b, 3), ...` instead. – Kevin Mar 10 '15 at 13:35