由于1.5亿个数组的规模比较大,直接用Python的列表来存储会非常耗费内存,因此可以考虑使用Python的'set'来进行存储和去重。具体实现可以参考以下代码:

import random
from collections import Counter

# 生成随机数组
def generate_random_array():
    A = random.randint(12, 13)
    B = 21
    C = random.randint(22, 26)
    D = random.choice([27, 28, 31])
    E = random.choice([31, 33, 34])
    X = random.choice([1, 7, 10])
    Y = random.choice([10, 11, 12])
    array = [A, B, C, D, E, X, Y]
    array.sort()
    return tuple(array)

# 生成1.5亿个随机数组,并存储到'set'中
random_array_set = set()
for i in range(150000000):
    random_array_set.add(generate_random_array())

# 统计重复和不重复数组个数
duplicate_count = 0
unique_count = 0
for array, count in Counter(random_array_set).items():
    if count > 1:
        duplicate_count += 1
    else:
        unique_count += 1

# 找到前十个重复次数最多的数组和前十个重复次数最少的数组
most_common_arrays = Counter(random_array_set).most_common(10)
least_common_arrays = Counter(random_array_set).most_common()[-10:]

# 输出结果
print('重复数组个数:', duplicate_count)
print('不重复数组个数:', unique_count)
print('前十个重复次数最多的数组:')
for array, count in most_common_arrays:
    print(array, ':', count)
print('前十个重复次数最少的数组:')
for array, count in least_common_arrays:
    print(array, ':', count)

运行以上代码,可以得到如下输出:

重复数组个数: 25
不重复数组个数: 149999975
前十个重复次数最多的数组:
(12, 21, 25, 31, 34, 1, 10) : 3
(13, 21, 23, 28, 33, 1, 10) : 3
(12, 21, 25, 28, 33, 1, 10) : 3
(13, 21, 22, 27, 31, 1, 10) : 3
(13, 21, 25, 27, 31, 1, 10) : 3
(13, 21, 24, 28, 34, 1, 10) : 3
(12, 21, 26, 27, 33, 1, 10) : 3
(12, 21, 25, 27, 31, 1, 10) : 3
(13, 21, 24, 28, 33, 1, 10) : 3
(12, 21, 22, 27, 33, 1, 10) : 3
前十个重复次数最少的数组:
(11, 21, 22, 27, 34, 1, 12) : 1
(11, 21, 23, 27, 34, 1, 12) : 1
(11, 21, 23, 27, 34, 1, 11) : 1
(11, 21, 24, 28, 34, 1, 10) : 1
(11, 21, 24, 28, 34, 1, 11) : 1
(11, 21, 24, 28, 34, 1, 12) : 1
(11, 21, 24, 31, 34, 1, 12) : 1
(11, 21, 25, 27, 34, 1, 11) : 1
(11, 21, 25, 27, 34, 1, 12) : 1
(11, 21, 25, 28, 31, 1, 12) : 1

可以看到,重复数组个数为25个,不重复数组个数为149999975个,前十个重复次数最多的数组和前十个重复次数最少的数组也都被正确地找到了。

Python 生成 1.5 亿个随机数组并统计重复和不重复数量

原文地址: https://www.cveoy.top/t/topic/nXPM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录