【问题标题】:Generating a random number from highest count numbers in a list of tuples in Python从 Python 中的元组列表中的最高计数数字生成随机数
【发布时间】: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


【解决方案1】:

你的问题有点不清楚。你可以试试:

from heapq import nlargest
from random import choice

pairs = [(24, 11),
 (12, 10), (48, 10),
 ...
 (28, 2)
]

top_counts = set(nlargest(6, set(count for _, count in pairs)))
top_counts_numbers = [
    number for number, count in pairs if count in top_counts
]
print(choice(top_counts_numbers))
  • 第一部分使用heapqs nlargest()得到最大的6个计数:{6, 7, 8, 9, 10, 11}。正如其他人所提到的,这并不完全是您提供的计数。你这里的措辞有点模糊。你也可以使用sorted() 来做到这一点。 (我将结果转换为集合,因为集合提供了快速查找,这将在下一步中完成。但您不需要它。)
  • 通过列表推导选择相应的数字:
    [24, 12, 48, 10, 26, 59, 39, 53, 21, 52, 50, 41, 33, 44, 46, 38, 20, 57, 23,
    6, 3, 37, 51, 34, 54, 36, 14, 17, 58, 15, 29]
    
  • 使用choice() 从中选择一个随机数。

【讨论】:

  • 那!!!比我想出的要干净得多大声笑-我会用我的解决方案更新我的问题
  • 有了这个op_counts = set(nlargest(6, set(count for _, count in pairs))),它如何获得所有第二个索引?
  • @mrpbennett 这里count for _, count in pairs 元组被破坏(所谓的元组拆包} 分成2个变量,第一个命名为_,因为这里没有用到,第二个count被收集。
  • 啊,明白了,谢谢。我得做一些阅读。无论哪种方式,您的方式都更加清洁和快捷。谢谢
【解决方案2】:

我不确定以下解决方案是否能回答您的问题。我很困惑,因为前 6 个计数不包括 7 和 8 频率(而您似乎在上一条评论中暗示了这一点)。

代码根据第二个键对元组进行排序,然后随机选择其条目。

#!/usr/local/bin/python3
import random

# Tuplelist
TupleList = [(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)]

# Sort tuples
TupleList.sort(key = lambda x: x[1])

# Select most frequent tuples
NumberOfMaxElements = 6
MaxElements = TupleList[-NumberOfMaxElements:]
print("Most frequent tuples:")
print(MaxElements)

# Random draws
print("Some random draws:")
NumberOfValues = 20
for iter in range(NumberOfValues):
    RandomElement = random.randint(0, NumberOfMaxElements-1)
    RandomDraw = MaxElements[RandomElement][0]
    print(RandomDraw)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多