List Manipulation
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue Jul 4 15:42:14 EDT 2006
Roman a écrit :
<ot>
Roman, please stop top-posting and learn to quote.
</ot>
> Bruno Desthuilliers wrote:
>
>>Roman wrote:
>>(please dont top-post - corrected)
>>
>>>
>>>My intention is to create matrix based on parsed csv file. So, I
>>>would like to have a list of columns (which are also lists).
>>
>>csv = [
>> ['L0C0', 'L0C1', 'L0C2'],
>> ['L1C0', 'L1C1', 'L1C2'],
>> ['L2C0', 'L2C1', 'L2C2'],
>> ]
>>
>>matrix = [[[] for l in range(len(csv))] for c in range(len(csv[0]))]
>>
>>for numline, line in enumerate(csv):
>> for numcol, col in enumerate(line):
>> matrix[numcol][numline] = col
>>
>>assert matrix == [
>> ['L0C0', 'L1C0', 'L2C0'],
>> ['L0C1', 'L1C1', 'L2C1'],
>> ['L0C2', 'L1C2', 'L2C2']
>> ]
>>
> Thanks for spending so much time with me. I had since made the
> following change.
>
> matrix = [[[] for l in range(len(list(reader)[:10]))] for c in
> range(len(list(reader)[7]))]
Technically; this won't work. The first call to list(reader) will
consume reader.
It's also somewhat dumb (len(list(reader)[:10]) is always 10) and
inefficient (you're calling list(reader) twice, when you could call it
just once).
Instead of trying anything at random, read the fine manual and try to
understand the example I gave you.
> for numline, line in enumerate(reader):
> for numcol, col in enumerate(line):
> matrix[numcol][numline] = col
>
> print matrix
>
> I don't get mistakes anymore. However, still nothing gets printed
If you don't print anything, nothing will be printed.
More information about the Python-list
mailing list