0

I wrote the following bit of code:

...
for x in range(len(coeff)): coeff[x].insert(0,names[x])
coeff.insert(0,['Center','c1','c2','c3'])    
print_matrix(coeff)
...

The print_matrix function just prints a nice matrix from a tuple [[row1],[row2],[etc...]]. my coeff = [[1,2,3],[4,5,6]] and my names = ['A,'B'].

The first time i run the function I get:

coeff = [['Center','c1','c2','c3'],['A',1,2,3],[B,4,5,6]]
+----------------------+
| Center  c1  c2  c3   |
|  A      1   2   3    |
|  B      4   5   6    |
+----------------------+

which is exactly what I want. The problem starts when I run THE SAME (copied and pasted) script just after the first one to print in a similar fashion another tuple basis = [[7,8,9],[10,11,12]]:

...
del x
for x in range(len(basis)): basis[x].insert(0,names[x])
basis.insert(0,['Center','A1','A2','A3'])    
print_matrix(basis)
...

I then get:

basis = [['Center','A1','A2','A3'],['A','B',7,8,9],['A','B',10,11,12]]

and an error from the print_matrix functions since it doesn't get a tuple with equal lenght rows. Why?

MarcinKonowalczyk
  • 2,577
  • 4
  • 20
  • 26
  • 4
    Don't loop by index - it's slow, hard to read, and inflexible. `for x in range(len(coeff)): coeff[x].insert(0,names[x])` becomes `for x, name in zip(coeff, names): x.insert(0, name)`. You should also avoid using one line for loops - it makes code harder to read. If you are always inserting to the front, you might want to use a [`collections.deque`](http://docs.python.org/3.3/library/collections.html?highlight=deque#collections.deque) and `deque.appendleft()` over a list and `list.insert()` too. – Gareth Latty Jul 17 '13 at 09:35
  • @Lattyware Thank you for your syntax correction. It is much cleaner now but it still doesn't work. The results are exactly the same as with my loop design. I'm gonna try `deque` now. – MarcinKonowalczyk Jul 17 '13 at 09:47
  • Neither of the things I suggested will change the functionality of the program, they are simply better ways to do what you are doing. – Gareth Latty Jul 17 '13 at 09:54
  • You did not exactly copy&paste, or the implementation of `print_matrix` might be relevant. Here the code as you posted gives the correct result you want: `basis = [['Center', 'A1', 'A2', 'A3'], ['A', 7, 8, 9], ['B', 10, 11, 12]]` – Ludo Jul 17 '13 at 10:15

1 Answers1

0

Ok, I worked it out. What happened was that the way basis was constructed in the fist place affected the functions. I just gave random numbers as an example of basis but in fact it was (deep in the code):

coordinates = [...,[1,2,3],...]
coordinates[7] = [1,2,3] # Or something like that
basis = []
basis.append(coordinates[7])
...
basis.append(coordinates[7])

so that when I did insert(0,something) on basis[0], it also inserted element into basis[1].

Here is a strip of code that works:

...
basis_clone = [[y for y in basis[x]] for x in range(len(basis))]
for y, name in zip(basis_clone,orbital_center_names): y.insert(0,name)
basis_clone.insert(0,['Center','A1','A2','A3'])
print_matrix(basis_clone) ; sleep(0.1)
...

None of the methods given here worked so I had to clone the basis in the way I did. I'm open for suggestions of a better way to do that though.

P.S.: Thank you to @Lattyware for help on good syntax.

Community
  • 1
  • 1
MarcinKonowalczyk
  • 2,577
  • 4
  • 20
  • 26