The code below results in (for me) highly surprising output:
cols = [ int(x) for x in sys.argv[2:] ]
data = [[]] * len(cols)
with open( sys.argv[1] ) as f:
for l in f:
c = l.split()
if len(c) <= cols[-1]: continue
for i in range(0,len(cols)):
print( i, cols[i], c[cols[i]] )
data[i].append( float(c[cols[i]]) )
print()
for i in range( 0, len(cols)):
print( data[i] )
Calling this with e.g.:
python3 script.py file.txt 2 3
Where 'file.txt' contains:
1 0 0 -21612.9
2 0.0914607 0.0914607 -21611.6
...
The output of the first print is as expected like this:
0 2 0
1 3 -21612.9
0 2 0.0914607
1 3 -21611.6
...
However the second loop returned two identical lists like this:
[0.0, -21612.9, 0.0914607, -21611.6, ...
I expected two lists:
[0.0, 0.0914607, ...
[-21612.9, -21611.6, ...
I know that this can be done using e.g. defaultdict, but I would like to understand why that code does not work. I suspect it is the declaration of 'data' as fixed size list of empty lists?