A
A
Alexander Volkov2021-05-10 15:28:56
JavaScript
Alexander Volkov, 2021-05-10 15:28:56

Write a program that swaps any two columns in an array, how to fix it?

Given a two-dimensional array. Write a program that swaps any two columns in
an
array

matrix = 
 
print(matrix)
 
N = 4
 
for i in range(N):
  c = matrix[i][2]
  matrix[i][2] = matrix[i][4]
  matrix[i][4] = c
 
print(matrix)

Error in console


matrix[i][2] = matrix[i][4]
IndexError: list index out of range


Help me please)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Sartakov, 2018-10-31
@Nohaga

You need to include jquery before select2

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

jsfiddle.net/hfea7ng0

D
Danya Kozlovskiy, 2021-05-10
@A1exWolf

Of course, I'm not strong in python, but arrays are indexed from 0, i.e. if you have 4 elements in the array, then they will be at indexes 0,1,2,3. And in these lines you have 4 indexes that do not exist in this array, which is what the error tells you

IndexError: list index out of range
:
for i in range(N):
  c = matrix[i][2]
  matrix[i][2] = matrix[i][4] // выход за пределы массива
  matrix[i][4] = c  // выход за пределы массива

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question