如何使我的梦幻足球彩票脚本更加Pythonic?

时间:2016-12-31 02:23:45

标签: python python-2.7

我已经建立了一个简单的脚本,从我们的梦幻足球专员预先选择的彩票号码列表中选择一个号码。根据今年幻想足球比赛的结果,每支球队根据他们在常规赛中表现得如何好或差,可以获得指定数量的机会来选择彩票号码。

问题在于,我写过的脚本会选择一个团队的所有数字,然后再转移到下一个团队。我希望脚本能够在团队列表中一次选择一个号码。由于并非所有团队都有相同数量的机会选择彩票号码,因此复杂程度会增加。

所以我使用每个团队的计数器减少到零,这意味着他们已经选择了分配给他们的所有数字。我不确定如何让这个脚本一次挑选一个号码。

这是我的代码:

import random

lottery_numbers = ['1234','1235','1236','1237','1238','1239',
'1245','1246','1247','1248','1249','1256','1257','1258','1259',
'1267','1268','1269','1278','1279','1289','1345','1346','1347',
'1348','1349','1356','1357','1358','1359','1367','1368','1369',
'1378','1379','1389','1456','1457','1458','1459','1467','1468',
'1469','1478','1479','1489','1567','1568','1569','1578','1579',
'1589','1678','1679','1689','1789','2345','2346','2347','2348',
'2349','2356','2357','2358','2359','2367','2368','2369','2378',
'2379','2389','2456','2457','2458','2459','2467','2468','2469',
'2478','2479','2489','2567','2568','2569','2578','2579','2589',
'2678','2679','2689','2789','3456','3457','3458','3459','3467',
'3468','3469','3478','3479','3489','3567','3568','3569','3578',
'3579','3589','3678','3679','3689','3789','4567','4568','4569',
'4578','4579','4589','4678','4679','4689','4789','5678','5679',
'5689','5789','6789']

#Lists below will hold the final picks per the random selector and chances defined. 
lori = []
danny = []
joon = []
alex = []
abepat = []
jonny = []
jung = []
teho = []
jess = []

#Chances at lottery numbers used as counters.
HayanYujaMarinara = 30
hyobbes = 24
swoop = 19
alsizzle = 15
pabe = 12
youngbuck = 10
grimey = 8
hotay = 5
sopower = 3

#Lotto function to randomly select numbers and assign to general manager lists defined above. 
def lotto():
    #Define lottery pick chances per commissioner.
    while HayanYujaMarinara != 0:
        strong_island = random.choice(lottery_numbers)
        lori.append(strong_island)
        lottery_numbers.remove(strong_island)
        HayanYujaMarinara -= 1

    while hyobbes != 0:
        d4l = random.choice(lottery_numbers)
        danny.append(d4l)
        lottery_numbers.remove(d4l)
        hyobbes -= 1

    while swoop != 0:
        commish = random.choice(lottery_numbers)
        joon.append(commish)
        lottery_numbers.remove(commish)
        swoop -= 1

    while alsizzle != 0:
        texas = random.choice(lottery_numbers)
        alex.append(texas)
        lottery_numbers.remove(texas)
        alsizzle -= 1

    while pabe != 0:
        BestFriendsForever = random.choice(lottery_numbers)
        abepat.append(BestFriendsForever)
        lottery_numbers.remove(BestFriendsForever)
        pabe -= 1

    while youngbuck != 0:
        oldbuck = random.choice(lottery_numbers)
        jonny.append(oldbuck)
        lottery_numbers.remove(oldbuck)
        youngbuck -= 1

    while grimey != 0:
        LittleKidLover = random.choice(lottery_numbers)
        jung.append(LittleKidLover)
        lottery_numbers.remove(LittleKidLover)
        grimey -= 1

    while hotay != 0:
        amazebooths = random.choice(lottery_numbers)
        teho.append(amazebooths)
        lottery_numbers.remove(amazebooths)
        hotay -= 1

    while sopower != 0:
        soWeak = random.choice(lottery_numbers)
        jess.append(soWeak)
        lottery_numbers.remove(soWeak)
        sopower -= 1

if __name__ == '__main__':
    lotto()
print "[+]" + "Lori's lottery numbers are \n" + str(sorted(lori, key=int))
print "**********************************************************"
print "[+]" + "Danny's lottery numbers are \n" + str(sorted(danny, key=int))
print "**********************************************************"
print "[+]" + "Joon's lottery numbers are \n" + str(sorted(joon, key=int))
print "**********************************************************"
print "[+]" + "Alex's lottery numbers are \n" + str(sorted(alex, key=int))
print "**********************************************************"
print "[+]" + "Pabe's lottery numbers are \n" + str(sorted(abepat, key=int))
print "**********************************************************"
print "[+]" + "Jonny's lottery numbers are \n" + str(sorted(jonny, key=int))
print "**********************************************************"
print "[+]" + "Jung's lottery numbers are \n" + str(sorted(jung, key=int))
print "**********************************************************"
print "[+]" + "Teho's lottery numbers are \n" + str(sorted(teho, key=int))
print "**********************************************************"
print "[+]" + "Jess's lottery numbers are \n" + str(sorted(jess, key=int))
print "**********************************************************"
print "[xXx]" + "Remaining lottery numbers \n" + str(sorted(lottery_numbers, key=int))`

2 个答案:

答案 0 :(得分:1)

在数学上,首先为给予团队分配所有数字或者循环赛没有区别。至少,只要您的数字是随机选择的,那就是真的,这似乎是(如果效率低下)。所以你在技术上不需要改变任何东西。

但是如果你想改变一些事情,那么你可以采用更优雅的方式。首先,为你联盟中的每个人使用单独的变量是非常尴尬的(你应该使用一个列表!)。一个很大的性能改进是将数字列表重新排序一次,然后按顺序从中获取值(而不是选择随机项并需要从列表中间删除它们)。

这是我使用the roundrobin recipe from the itertools documentation快速(经过最低限度测试)尝试解决此问题的方法:

from random import shuffle
from itertools import repeat, cycle, islice
from collections import OrderedDict, defaultdict

def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    pending = len(iterables)
    nexts = cycle(iter(it).next for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = cycle(islice(nexts, pending))

lottery_numbers = ['1234','1235','1236','1237','1238','1239',
'1245','1246','1247','1248','1249','1256','1257','1258','1259',
'1267','1268','1269','1278','1279','1289','1345','1346','1347',
'1348','1349','1356','1357','1358','1359','1367','1368','1369',
'1378','1379','1389','1456','1457','1458','1459','1467','1468',
'1469','1478','1479','1489','1567','1568','1569','1578','1579',
'1589','1678','1679','1689','1789','2345','2346','2347','2348',
'2349','2356','2357','2358','2359','2367','2368','2369','2378',
'2379','2389','2456','2457','2458','2459','2467','2468','2469',
'2478','2479','2489','2567','2568','2569','2578','2579','2589',
'2678','2679','2689','2789','3456','3457','3458','3459','3467',
'3468','3469','3478','3479','3489','3567','3568','3569','3578',
'3579','3589','3678','3679','3689','3789','4567','4568','4569',
'4578','4579','4589','4678','4679','4689','4789','5678','5679',
'5689','5789','6789'] # this could probably be automatically generated

def lotto(draws_dict):
    results = defaultdict(list)

    numbers = list(lottery_numbers) # copy
    shuffle(numbers) # shuffles in place
    num_iter = iter(numbers)

    draw_sequence = roundrobin(*(repeat(player, count)
                                 for player, count in draws_dict.items()))

    for i, (player, numbers) in enumerate(zip(draw_sequence, num_iter)):
        print("Draw #{}, for {}: {}".format(i+1, player, number))
        results[player].append(number)

    print("\nFinal results:")
    for player, draws in results.items():
        print("{}: {}".format(player, ", ".join(map(str, sorted(draws)))))

    print("Unselected numbers: {}".format(", ".join(num_iter)))

    return results

if __name__ == "__main__":
    num_draws = OrderedDict([('lori', 30), ('danny', 24), ('joon', 19),
                             ('alex', 15), ('abepat', 12), ('jonny', 10),
                             ('jung', 8), ('teho', 5), ('jess', 3)])
    results = lotto(num_draws)

答案 1 :(得分:0)

随机选择不需要循环法 你真的应该考虑分解你的公共代码。此外,您可能需要考虑不同的数据结构 - 即dict

import random
chances = [('lori', 30), ('danny', 24), ...]
random.shuffle(lottery_numbers)

lotto = {name: [lottery_numbers.pop() for _ in range(no_tickets)] for name, no_tickets in chances}

for name, tickets in lotto.items():
    print("[+] {}'s lottery numbers are".format(name))
    print(' '.join(str(t) for t in sorted(tickets)))
    print('****')
print("[xXx] Remaining lottery numbers")
print(' '.join(str(t) for t in sorted(lottery_tickets)))

输出:

[+] lori's lottery numbers are
1237 1238 1259 1267 1269 1279 1345 1359 1378 1389 1458 1459 1789 2346 2347 2348 2378 2456 2458 2459 2468 2478 2489 3467 3469 3689 3789 4679 4789 5678
****
[+] danny's lottery numbers are
1236 1246 1278 1379 1479 1578 1589 1678 1689 2379 2467 2567 2678 2679 2789 3567 3569 3579 3589 3678 4568 4569 4578 5689
...