There is a task: "To form a matrix of numbers from 0 to 999, display it on the screen. Count the number of two-digit numbers in it." I’m trying to set a matrix. I’m killing this: a [i] [j] = int (a [i] [j]) IndexError: list index out of range How to fix ?. Thank you very much!

import random a=[] for i in range(10): for j in range(10): a[i][j]=int(a[i][j]) a.append(random.randint(0,999)) count=0 if(10 <= a[i][j]<= 99): count += 1 print(a) print('количество двузначных чисел',count) 

    2 answers 2

    To be honest, you have written something very strange.

    The code a = [] creates a list, and from the condition it is assumed that this will be a matrix (that is, a list of lists). A 10 × 10 matrix of zeros can be formed using a generator:

     a = [[0]*10 for i in range(10)] 

    Now its elements can be accessed as a[i][j] . Further, with its bypass everything is clear, two nested loops. Everything is good here. But it’s completely unclear from your code what you wanted to do with each element:

     a[i][j]=int(a[i][j]) 

    from the position [i, j] matrix a takes a value, converts it to a number and saves it to the same position. Perhaps you needed some other effect. If we assume that you were going to initialize the matrix element in this way, then now it is not necessary, since this has already been done. Go ahead:

     a.append(random.randint(0,999)) 

    This construction adds an element to the list (at its end!) By writing a random number there. And this means that it changes the size of the list, which is unacceptable in this case. If you thus wanted to write a random number as the current element, you had to refer to the current element and not create a new one:

     a[i][j] = random.randint(0,999) 

    The next line you reset the value of the variable count . So nothing will come out, you do it at each iteration of the loop (and therefore never count 1 is stored in the count ), and if you want to bypass all the elements of the matrix and count their number, the counter reset code must be removed from the loop by placing it . With the conditional operator everything is fine.

    So, if I correctly understood what was meant (although for me, this is a strange interpretation of the problem condition), the code will turn out like this:

     import random a = [[0]*10 for i in range(10)] count = 0 for i in range(10): for j in range(10): a[i][j] = random.randint(0,999) if 10 <= a[i][j] <= 99: count += 1 print(a) print('количество двузначных чисел', count) 

    Or, if you write shorter:

     a = [[random.randint(0,999) for i in range(10)] for j in range(10)] count = sum([1 for i in range(10) for j in range(10) if 10 <= a[i][j]<= 99]) 
    • I thank you for the detailed answer, I wanted the whole process to look like this: the number a.append(random.randint(0,999)) is selected, then assign it a number by i and by j , as in the true matrix, then here if(10 <= a[i][j]<= 99): checked if there is an etot element of two digits. If there is, then the counter is +1. - Awesome Man
    • one
      For numpy arrays: a = numpy.random.random_integers(0, 999, (10,10)); count = ((10 <= a) & (a <= 99)).sum() a = numpy.random.random_integers(0, 999, (10,10)); count = ((10 <= a) & (a <= 99)).sum() (there is also numpy.count_nonzero((10 <= a) & (a <= 99)) if you want to express the intention of expressing the code more clearly) . - jfs
    • @jfs, why not add this as an answer? Your decision can certainly be useful to someone. In addition, it will be useful to those who study NumPy ... - MaxU
    • @MaxU my comment is not related to the "list index out of range" question (the current answer does that well). The comment answers the question: "count the number of two-digit numbers in a 10x10 matrix of random numbers in the range from 0 to 999 inclusive." If you wish, then of course you can post this code as an answer or as a new question and answer that you think is better. - jfs

    Here

     a[i][j]=int(a[i][j]) 

    Your "a" is still an empty [] list. And you are already trying to access its elements.

    And in general, the code can be written much more concisely, if instead of cycles we use list inclusions:

     from random import randint from itertools import chain a = [[randint(0, 999) for _ in range(10)] for _ in range(10)] bi = [i for i in chain(*a) if 9 < i < 100] print(a) print(len(bi)) 
    • Thank you very much! But I'm still a novice, even before itertools did not reach) - Awesome Man
    • one
      You can do with one for X in .. - a = [random.sample(range(0, 999), 10) for _ in range(10)] - MaxU
    • @MaxU, only the result will be slightly different. sample each sequence element uses no more than once. And from the author's code it follows that the numbers may well be repeated. - Xander
    • @ Alexander, for sure! I missed this moment ... - MaxU