N,M = map(int, input().split()) a = map(int, input().split()) b = [0] * N c = 0 d = 0 e = [] for i in range (M): b[a[i]] += 1 for i in range (N + 1): if b[i] >= c: c = b[i] e[d] = i d += 1 print(*e) 

For some reason, an error appears in the line b [a [i]], although b is an array created from zeros and in principle it seemed to me that everything should work

  • In the third python, the map call returns not a ready list, but a lazy generating object. This allows you to save memory and perform long calculations not at once in a large piece, but as needed. Therefore, if you need a list after all, then after the map you need to explicitly apply the list. - Xander

1 answer 1

When calling a[i] operator [i] translated into a call to the method a.__getitem__(i) .

An object of type map does not have such a method:

 In [14]: type(a) Out[14]: map In [15]: a.__getitem__(0) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-15-82c105a3393c> in <module> ----> 1 a.__getitem__(0) AttributeError: 'map' object has no attribute '__getitem__' 

Wrap all map() calls with the list() constructor:

 N,M = list(map(int, input().split())) a = list(map(int, input().split()))