#NEWLINE# def facetcolumns( table , key , missing = None ) : #NEWLINE# #INDENT# "\n    Like :func:`petl.util.materialise.columns` but stratified by values of the\n    given key field. E.g.::\n\n        >>> import petl as etl\n        >>> table = [['foo', 'bar', 'baz'],\n        ...          ['a', 1, True],\n        ...          ['b', 2, True],\n        ...          ['b', 3]]\n        >>> fc = etl.facetcolumns(table, 'foo')\n        >>> fc['a']\n        {'foo': ['a'], 'baz': [True], 'bar': [1]}\n        >>> fc['b']\n        {'foo': ['b', 'b'], 'baz': [True, None], 'bar': [2, 3]}\n\n    " #NEWLINE# fct = dict ( ) #NEWLINE# it = iter ( table ) #NEWLINE# hdr = next ( it ) #NEWLINE# flds = list ( map ( text_type , hdr ) ) #NEWLINE# indices = asindices ( hdr , key ) #NEWLINE# assert ( len ( indices ) > 0 ) , 'no key field selected' #NEWLINE# getkey = operator . itemgetter ( * indices ) #NEWLINE# for row in it : #NEWLINE# #INDENT# kv = getkey ( row ) #NEWLINE# if ( kv not in fct ) : #NEWLINE# #INDENT# cols = dict ( ) #NEWLINE# for f in flds : #NEWLINE# #INDENT# cols [ f ] = list ( ) #NEWLINE# #UNINDENT# fct [ kv ] = cols #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# cols = fct [ kv ] #NEWLINE# #UNINDENT# for ( f , v ) in izip_longest ( flds , row , fillvalue = missing ) : #NEWLINE# #INDENT# if ( f in cols ) : #NEWLINE# #INDENT# cols [ f ] . append ( v ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return fct