【问题标题】:Function that extracts random numbers and progressively excludes the ones that generates提取随机数并逐步排除生成随机数的函数
【发布时间】:2019-11-06 14:37:13
【问题描述】:

我正在尝试做一个彩票提取器,我需要一个脚本来提取随机数并存储提取的随机数以不再提取它们。

我想出了将提取的数字存储在文本文件 (ToExclude.txt) 中并将该文件用作“提取”变量的想法。

我尝试这样做没有成功:

import random

f = open("ToExclude.txt", "r") #Opens the txt in read mode
NumToExclude = f.read() #Stores the txt file (with the previous extracted numbers)
f.close()
open("ToExclude.txt", "w").close() #Erases the content of the file

number = random.randint(1,100) #Random number generator

if number in NumToExclude: #Verifies that the number is not in the Extracted list
    [RESTART FUNCTION] #Restarts the function to generate another number
else:
    NumToExclude.append(number) #If is not in the list add the number to the lsit

print("The number is: {}".format(number)) #Example of output

f = open("ToExclude.txt", "w")  #Stores the extracted number in the txt file
f.write(str(NumToExclude))
f.close()

当我执行它时,第一个运行良好,但在第二个之后,文本文件如下所示:

['[', '5', '7', ',', ' ', '5', '7', ']', 67, 67]

【问题讨论】:

  • random.sample(range(100),x ) 将返回 x 未被替换的随机数。
  • random.sample(range(1,100),x) 与问题中相同的数字(从 1 到 99)
  • NumToExclude = f.read() 将文本字符串放入 NumToExclude,但您需要一个数字列表
  • NumToExclude= [ast.literal_eval(line.strip()) for line in f]` 如stackoverflow.com/questions/33351770/…
  • 随机播放您的号码列表,然后从随机播放的列表中选择您想要的号码。这保证没有重复,前提是初始列表没有重复。

标签: python python-3.x random


【解决方案1】:

你想多了。你可以这样做:

all_numbers = list(range(1,101))

extracted = random.choice(all_numbers)
all_numbers.remove(extracted)

如果需要,您可以循环浏览最后两行并将值也保存在列表中:

all_numbers = list(range(1,101))
result =[]
for i in range(10):
    extracted = random.choice(all_numbers)
    result.append(extracted)
    all_numbers.remove(extracted)

【讨论】:

  • 但是我需要一遍又一遍地运行脚本,当我重新运行脚本时,变量“Extracted”不会重置吗?
  • @TobiaGiampiccolo 但为什么需要重新运行脚本?最终目标是什么?您可以一次性提取它们,如果您想在每次运行脚本时提取它们 1,是的,您需要将数据保存在文本文件中。
【解决方案2】:

这里有很多问题。首先,您不能只是将列表转储到文本文件中,然后将其作为列表读回,这就是您出现奇怪行为的原因。您应该查看一个名为 pickle 的模块。其次,正如刚刚出现的另一个答案指出的那样,您不需要所有这些复杂性。只需从所有数字的列表开始,然后使用random.choice() 从中进行选择。第三,最好在 with 块中打开文本文件,这样如果发生异常,它们将被正确关闭。

with f as open('path','r'):
 f.read()

【讨论】:

    【解决方案3】:

    当您擦除文件时,我认为您不需要长期保留它。 可以使用集合,是不可变结构,不允许重复。

    import random
    
    lottery = set() #empty set
    

    假设您要生成 6 个数字:

    while len(lottery) < 7:
    
        lottery.add(random.randint(1,100)) #same method you used for random
    
    print(lottery)
    {6, 41, 75, 49, 85, 87, 61}
    

    如果需要多次运行,放入一个函数中

    def run_lottery():
    
        lottery = set()
    
            while len(lottery) < 7:
                    lottery.add(random.randint(1,100)) #same method you used
    
        return lottery
    

    并用变量调用它:

      lucky = run_lottery()
    
      print(lucky)
      {100, 10, 13, 17, 49, 57, 63}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多