如何从4位数列表中查找最频繁的累进位数

2024-04-19 10:09:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python编程相当陌生。从4位数字列表中查找最频繁的累进数字的有效方法是什么

假设我有以下列表:[6111, 7111, 6112, 6121, 6115, 6123]

逻辑是观察到第一位数字6的频率最高。为了下一步考虑,我可以去掉7111这个数字

对于第二个数字,我考虑新的候选{{CD2>},并且观察到1是最频繁的数字等等。p>

在算法结束时,我只剩下列表中的1个数字

如果一个数字有两个或两个以上出现相同的数字,我可以在所有数字中随机选取较小的数字

一种简单的方法可以将列表转换成NX4矩阵,并为每个列考虑最频繁的数字。这可能会奏效,但我找到了一个非常愚蠢和低效的方法来解决这个问题。有人能帮忙吗

编辑:此解决方案的我的代码(注意:此代码并不总是有效,有些地方出错。有关此问题的解决方案,请参阅@MadPhysical ANSWER)

import numpy as np
import pandas as pd
from collections import Counter



numbers_list = [6111, 7111, 6112, 6121, 6115, 6123]

my_list = []

for number in numbers_list:
    digit_list = []
    for c in str(number):
       digit_list.append(c)
    my_list.append(digit_list)


matrix = np.array(my_list)

matrix0 = matrix

my_counter = Counter(matrix.T[0]).most_common(1)
i=0
for digit0 in matrix.T[0]:
    if digit0 != my_counter[0][0]:
        matrix0 = np.delete(matrix, i, 0)
    i += 1
matrix = matrix0

matrix1 = matrix
my_counter = Counter(matrix.T[1]).most_common(1)
i=0
for digit1 in matrix.T[1]:
    if digit1 != my_counter[0][0]:
        matrix1 = np.delete(matrix, i, 0)
    i += 1
matrix = matrix1

matrix2 = matrix
my_counter = Counter(matrix.T[2]).most_common(1)
i=0
for digit2 in matrix.T[2]:
    if digit2 != my_counter[0][0]:
        matrix2 = np.delete(matrix, i, 0)
    i += 1

matrix = matrix2

matrix3 = matrix
my_counter = Counter(matrix.T[3]).most_common(1)
i=0
for digit3 in matrix.T[3]:
    if digit3 != my_counter[0][0]:
        matrix3 = np.delete(matrix, i, 0)
    i += 1
matrix = matrix3

print (matrix[0])

Tags: 方法inmost列表forifmynp
1条回答
网友
1楼 · 发布于 2024-04-19 10:09:06

您转换为numpy数组的想法是可靠的。你不需要提前把它分开。一系列的遮罩和直方图将相当快地缩小阵列

z = np.array([6111, 7111, 6112, 6121, 6115, 6123])

第n个数字(从零开始)可以通过以下方式获得

nth = (z // 10**n) % 10

使用^{}可以快速计算最频繁的次数,如here所示:

frequentest = np.argmax(np.bincount(nth))

您可以简单地选择第n位有该数字的元素

mask = nth == frequentest

现在在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]]

碰巧,^{}将返回第一个最大计数的索引(如果有多个可用),这意味着它将始终选择最小的数字

您可以使用以下方法从output恢复数字

>>> output
array([1, 1, 1, 6])
>>> (output * 10**np.arange(output.size)).sum()
6111

您还可以只获取z的剩余元素:

>>> z[0]
6111

相关问题 更多 >