Ваша идея преобразования в массив numpy - solid. Вам не нужно разделять его заранее. Серия масок и гистограмм довольно быстро сократит массив.
z = np.array([6111, 7111, 6112, 6121, 6115, 6123])
n-ые цифры (отсчитываемые от нуля) могут быть получены примерно с можно быстро выполнить с помощью np.bincount
, как показано здесь :
frequentest = np.argmax(np.bincount(nth))
Вы можете выбрать элементы, которые имеют это di git на n-м месте просто
mask = nth == frequentest
Итак, теперь запустите это в al oop поверх n
(в обратном направлении):
# Input array
z = np.array([6111, 7111, 6112, 6121, 6115, 6123])
# Compute the maximum number of decimal digits in the list.
# You can just manually set this to 4 if you prefer
n = int(np.ceil(np.log10(z + 1).max()))
# Empty output array
output = np.empty(n, dtype=int)
# Loop over the number of digits in reverse.
# In this case, i will be 3, 2, 1, 0.
for i in range(n - 1, -1, -1):
# Get the ith digit from each element of z
# The operators //, ** and % are vectorized: they operate
# on each element of an array to return an array
ith = (z // 10**i) % 10
# Count the number of occurrences of each number 0-9 in the ith digit
# Bincount returns an array of 10 elements. counts[0] is the number of 0s,
# counts[1] is the number of 1s, ..., counts[9] is the number of 9s
counts = np.bincount(ith)
# argmax finds the index of the maximum element: the digit with the
# highest count
output[i] = np.argmax(counts)
# Trim down the array to numbers that have the requested digit in the
# right place. ith == output[i] is a boolean mask. It is 1 where ith
# is the most common digit and 0 where it is not. Indexing with such a
# mask selects the elements at locations that are non-zero.
z = z[ith == output[i]]
Как это происходит, np.argmax
вернет индекс первого максимального числа, если доступно несколько, что означает, что он всегда будет выбирать наименьшее число.
Вы можете восстановить число из output
с помощью чего-то вроде
>>> output
array([1, 1, 1, 6])
>>> (output * 10**np.arange(output.size)).sum()
6111
Вы также можете просто получить оставшийся элемент z
:
>>> z[0]
6111