0

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?

redo
  • 25
  • 5
  • Got the answer as to why my definition behaves strangely from the linked question: it creates references to the same object. – redo Nov 27 '19 at 13:07

1 Answers1

1

change data = [[]] * len(cols) to data = [[] for i in range(len(cols))] and keep everything same.

sahasrara62
  • 10,069
  • 3
  • 29
  • 44