我开始对列表中的前两个进行编程:
import random
def lottery_six():
setOfSix = set()
while len(setOfSix) < 6:
setOfSix.add(random.randint(1,49))
lottery = list(setOfSix)
return lottery
def generateLottery(lottery):
abc = set()
while (all(i >= 25 for i in lottery) == True) or (all(i < 25 for i in lottery) == True) or \
(sum(i >= 25 for i in lottery) >= 5) or (sum(i < 25 for i in lottery) >= 5):
abc = lottery_six()
return abc
print(generateLottery(lottery_six()))
但是,这行不通。为什么?我该如何解决它?
考虑到这一点,我们将重复此代码,直到找到一组合适的值。首先,我们将取范围 [1,49],然后对它们进行随机排序并取前 6 个值。然后我们检查范围是否满足前两个要求。如果是的话,我们会打破循环并保留这个值列表。
while True:
x = (np.random.permutation(49)+1)[0:6]
if len([i for i in x if 1<=i<25]) > 4: break
if len([i for i in x if 25<=i<=49]) > 4: break
print(x)
整个代码可以写成
while True:
x = (np.random.permutation(49)+1)[0:6]
# Check for short
if len([i for i in x if 1<=i<25]) > 4: break
# Check for large
if len([i for i in x if 25<=i<=49]) > 4: break
# Check for even
if len([i for i in x if i%2 == 0]) > 5: break
# Check for odd
if len([i for i in x if (i+1)%2 == 0]) > 5: break
# Check for successive
if len([i for ix, i in enumerate(x[0:-2])
if (x[ix+1] - i == 1 and x[ix+2] - x[ix+1] == 1)]) > 0: break
print(x)
这将找到满足您条件的列表。最后一条语句有点密集,分解后它会遍历列表中的每个值,并检查是否有至少 3 个连续值
x[ix+1] - i == 1 and x[ix+2] - x[ix+1] == 1
。如果这是真的,我们将值添加到列表中,如果最后这个新列表中至少有 1 个值,我们可以得出结论,至少有 3 个连续值。
import random
def lottery_six():
setOfSix = set()
while len(setOfSix) < 6:
setOfSix.add(random.randint(1,49))
lottery = list(setOfSix)
return lottery
def generateLottery(lottery):
abc = set(lottery) #Modified this Line
while (all(i >= 25 for i in lottery) == True) or (all(i < 25 for i in lottery) == True) or (sum(i >= 25 for i in lottery) >= 5) or (sum(i < 25 for i in lottery) >= 5):
abc = lottery_six()
return abc
print(generateLottery(lottery_six()))
你的代码好像没有进入循环,你可能在while循环之前生成了一个集合。而且这些行似乎是多余的:
(all(i >= 25 for i in lottery) == True) or (all(i < 25 for i in lottery) == True)
最终代码:
def generateLottery(lottery):
lottery = lottery_six()
while sum(i >= 25 for i in lottery) >= 5 or sum(i < 25 for i in lottery) >= 5:
lottery = lottery_six()
return lottery
print(generateLottery(lottery_six()))
中彩票是我梦想的一部分,我很努力地想赢得大奖但一切都无济于事,直到我在网上遇到了首席弗里多,他让我梦想成真,让我赢得了2200万美元。我是一名物流经理,住在南卡罗来纳州的兰开斯特,在距离北卡罗来纳州夏洛特约一小时车程的地方工作。午休期间,我在一家商店买了一张刮刮乐彩票,因为弗里多酋长给了我所有的保证在我做了他要求我做的所有事情之后,这些数字就不会失败。弗里多酋长是一位强大的弗里多酋长,他的使命是消除人们生活中的贫困,我已经确认,通过他为我提供的号码赢得了 2200 万美元,我承诺向世界讲述我与弗里多酋长的经历,这就是我现在正在做的,您可以在 frido 酋长的帮助下快速中奖,他经过测试且值得信赖电子邮件:[email protected] 或 WhatsApp +234 706 541 7981 他会帮助您。