Building a dict from a tuple of tuples

MRAB python at mrabarnett.plus.com
Sat Feb 20 13:00:59 EST 2010


vsoler wrote:
> Hello everyone!
> 
> I have a tuple of tuples, coming from an Excel range, such as this:
> 
> ((None, u'x', u'y'),
> (u'a', 1.0, 7.0),
> (u'b', None, 8.0))
> 
> I need to build a dictionary that has, as key, the row and column
> header.
> 
> For example:
> d={ (u'a',u'x'):1.0, (u'a',u'y'): 7.0, (u'b',u'y'):8.0 }
> 
> As you can see, if the value in the matrix is None, no key has to be
> added to the dictionary.
> 
> Of course, my tuple of tuples is a lot bigger.
> 
> How can I possibly do this?
> 
> Thank you

Does this help?

matrix = ((None, u'x', u'y'),
(u'a', 1.0, 7.0),
(u'b', None, 8.0))

for row in matrix[1 : ]:
     for col, val in zip(matrix[0][1 : ], row[1 : ]):
         print row[0], col, val



More information about the Python-list mailing list