Howto: extract a 'column' from a list of lists into a new list?
Max M
maxm at mxm.dk
Tue Jul 1 04:10:38 EDT 2003
Greg Brunet wrote:
> Even better yet... the reason I'm trying to do this is to make it easy
> to refer to a field by the field name as well as the field number. I
> expect to be reading all of the data into a list (1 per row/record) of
> row objects. If someone wants to be able to refer to FldA in record 53,
> then I'd like them to be able to use: "row[52].FldA" instead of having
> to use "row[52][4]" (if it's the 5th field in the row). I was planning
> on using the __getattr__ method in my row object like the following:
If that is all you want to do, this might be the simlest approach:
fields = [
('STOCKNO', 'C', 8, 0),
('DACC', 'C', 5, 0),
('DEALERACCE', 'C', 30, 0),
('D-ACCRTL', 'C', 9, 0),
('D-ACCCST', 'C', 9, 0)
]
def byName(field, name):
fieldNames = {
'name':0,
'letter':1,
'val1':2,
'val2':3,
}
return field[fieldNames[name]]
for field in fields:
print byName(field, 'name')
regards Max M
More information about the Python-list
mailing list