【发布时间】:2022-07-30 00:04:51
【问题描述】:
假设我有一个像这样的元组列表:
元组的第二个索引是第一个索引在数据集中出现的次数的计数。
[(24, 11),
(12, 10), (48, 10),
(10, 9), (26, 9), (59, 9), (39, 9), (53, 9), (21, 9), (52, 9), (50, 9),
(41, 8), (33, 8), (44, 8), (46, 8), (38, 8), (20, 8), (57, 8),
(23, 7), (6, 7), (3, 7), (37, 7), (51, 7),
(34, 6), (54, 6), (36, 6), (14, 6), (17, 6), (58, 6), (15, 6), (29, 6),
(13, 5), (32, 5), (9, 5), (40, 5), (45, 5), (1, 5), (31, 5), (11, 5), (30, 5), (5, 5), (56, 5), (35, 5), (47, 5),
(2, 4), (19, 4), (42, 4), (25, 4), (43, 4), (4, 4), (18, 4), (16, 4), (49, 4), (8, 4), (22, 4), (7, 4), (27, 4),
(55, 3),
(28, 2)]
例子
(24, 11) = (number, count)
如您所见,第二个索引中有多个相同数字。有没有办法收集前六个计数并将它们放入另一个列表中?
例如,收集所有 11、10、9、8、7 等计数,然后从该集合中生成长度为 6 的数字。
我正在尝试从 6 个最常见的数字中生成一个随机数。
更新
这就是我设法做到的方式
def get_highest_lotto_count(data) -> list:
\"\"\"Takes all numbers from 2nd index then extracts the top 6 numbers\"\"\"
numbers = data[\"lotto\"]
highest_count_numbers: list = [num[1] for num in numbers]
high_count_nums = list(set(highest_count_numbers))
high_count_nums.reverse()
return high_count_nums[:6]
data[\"lotto\"] 是上面提供的列表。我剥离了所有第二个索引号(计数)并转换为一组以删除重复项。
然后这给了我所有的计数,然后我从反向列表中取出前六个。
def common_lotto_generator() -> list:
\"\"\"
This takes the top 6 numbers from get_highest_lotto_count and generates a list
from the all the numbers that have the same 2nd index.
Then generates a random 6 digit number from the list.
\"\"\"
high_count_numbers = get_highest_lotto_count(collect_duplicate_lotto_numbers())
data = collect_duplicate_lotto_numbers()
numbers = data[\"lotto\"]
common_number_drawn: list = [
num[0] for num in numbers if num[1] in high_count_numbers
]
return random.sample(common_number_drawn, 6)
然后我调用上面的函数来获取 6 个数字的列表并再次添加数据,这样我就可以从 6 个列表中获取与第二个索引匹配的所有元组。
-
是的,当然有办法。你的问题到底是什么?
-
请为此问题添加一些输出
-
嗨,我正在努力理解预期的输出会是什么样子。你能提供吗?谢谢。
-
\"我正在尝试生成一个随机的最常见的 6 个数字中的数字。\" -> 如果它可以以可重现的方式生成,则它不是随机的。
-
如果您有 6 个计数为 11(最大值)的数字,您希望输出为这 6 个数字怎么办?
标签: python numpy random numbers