返回最低分

时间:2017-08-30 11:55:51

标签: python grouping min

这是我的代码,它会从多个成员中创建群组,然后对每个群组进行评分,然后将它们相加以得到这种情况下4个群组的总体得分。

import random

def run(members, n_groups):
    participants = list(range(1,members+1))*n_groups
    random.shuffle(participants)

    my_groups = list(zip(*[iter(participants)]*members))
    print(my_groups)

    def get_rating(group):
        return (len(set(group)))

    score = ((sum(get_rating(g) for g in my_groups)))
    print(score)
    return score

members = 4
n_groups = 4

print(min(run(members, n_groups) for _ in range(10)))

输出:

[(3, 3, 4, 1), (4, 2, 3, 1), (2, 3, 2, 4), (1, 1, 4, 2)]
13
[(3, 1, 1, 4), (2, 3, 2, 4), (1, 4, 1, 2), (3, 2, 4, 3)]
12
[(2, 4, 4, 1), (3, 3, 1, 3), (3, 1, 2, 2), (4, 4, 2, 1)]
11
[(3, 1, 4, 1), (3, 1, 2, 1), (2, 2, 4, 4), (3, 2, 3, 4)]
11
[(1, 1, 3, 1), (2, 2, 3, 1), (3, 2, 4, 4), (4, 4, 3, 2)]
11
[(3, 2, 1, 2), (4, 1, 3, 4), (4, 2, 3, 1), (1, 2, 4, 3)]
14
[(2, 4, 3, 1), (3, 4, 4, 2), (1, 1, 2, 1), (3, 3, 4, 2)]
12
[(3, 3, 1, 2), (2, 1, 3, 3), (2, 4, 4, 2), (4, 1, 1, 4)]
10
[(1, 4, 3, 2), (4, 2, 3, 3), (3, 1, 2, 1), (4, 4, 1, 2)]
13
[(2, 4, 1, 3), (3, 2, 2, 3), (1, 4, 1, 3), (1, 4, 2, 4)]
12
10

因此每组获得一个评分,然后最低分显示在底部,有没有办法可以重新打印获得该分数的组?因为如果我尝试使用100,000,那么我很难检查每一个以找到它来自哪个组!

我试过了:

import random

def run(my_groups, score):
    participants = list(range(1,members+1))*n_groups
    random.shuffle(participants)

    my_groups = list(zip(*[iter(participants)]*members))
    print(my_groups)

    def get_rating(group):
        return (len(set(group)))

    score = ((sum(get_rating(g) for g in my_groups)))
    print(score)
    return score

members = 4
n_groups = 4

print(min(run(members, n_groups) for _ in range(10), key=lambda x:x[1]))

但是这会返回“生成器表达式必须括号,如果不是唯一的参数”,则运行括号吗?

2 个答案:

答案 0 :(得分:5)

更改run以返回my_groups, score

然后改变

min(run(members, n_groups) for _ in range(10))

min((run(members, n_groups) for _ in range(10)), key=lambda x:x[1])

这使得min返回整个(my_groups, score)元组,但仅比较score(元组的第二个元素)以找到最小值。

答案 1 :(得分:1)

rsync -av -f"+ */" -f"- *" "$source" "$target"