Rearranging degrees of freedom

Hi, in a 2D in-plane elastic problem, I made a simple rectangular mesh and applied a homogenous Dirichlet BC on the left of the rectangle. The goal is to find stiffness and mass tensors. As I was not satisfied with the organization of elements in my stiffness matrix I decided to check how the degrees of freedom are distributed in the mesh. So I used:

Blockquote
gdim = mesh1.geometry().dim()
dofs_0 = V.dofmap().dofs()
dofs_x = V.tabulate_dof_coordinates()

and then I printed:

Blockquote
for dof, dof_x in zip(dofs_0, dofs_x):
print (dof, ‘:’, dof_x)

To see how the degrees of freedom are distributed on the mesh. The result is like:

But, what I need is to have an arrangement in a way that starting from the left-most column, the counter of degrees of freedom increase with the rows and then it goes to the next column and again increases with rows. Something like:

So, I decided to re-arrange the array, dofs_x, by writing a simple for loop like:

Blockquote
e=[1,2,3,4,5,6,7,8,9,9,9,8,7,6,5,4,3,2,1]
e=np.array(e)
a0=0
b=0
Nx=nx+1
Ny=ny+1
for i in range(0,Ny):
e1=e[i:i+11]
a0=ie1[0]
b=a0
print(i)
dofs_x[2
i]=YY[b]
dofs_x[2i+1]=YY[b]
for j in range(1,Nx):
if (i+j)<11:
q=a0+2
e1[j]
elif (i+j)>10:
q=a0+2e1[j-1]
a0=q
print(q)
dofs_x[18
j+2i]=YY[q]
dofs_x[18
j+2*i+1]=YY[q]

Now, my question is:

How can I feed this new arrangement of degrees of freedom to my solution and update my K and M matrix? My dofs_x vector is updated but still, the arrangement in the solution is as previous.