我有一个系统可以将随机组设为4,然后对它们进行评分:
import numpy
import math
import random
for i in range(1):
# everything is in, see the indentation
members=4
n_groups =4
participants=list(range(1,members+1))*n_groups
#print participants
random.shuffle(participants)
with open('myfile1.txt','w') as tf:
for i in range(n_groups):
group = participants[i*members:(i+1)*members]
for participant in group:
tf.write(str(participant)+' ')
tf.write('\n')
with open('myfile1.txt','r') as tf:
g = [list(map(int, line.split())) for line in tf.readlines()]
print(g)
my_groups =g
def get_rating(group):
return len(set(group))
for each_grp in my_groups:
print((get_rating(each_grp)))
这给出了输出:
[[1, 3, 2, 1], [4, 4, 3, 3], [2, 2, 1, 3], [1, 4, 4, 2]]
3
2
3
3
有没有办法可以总结4个等级来获得总分?就像在这种情况下(3 + 2 + 3 + 3)= 11,如果我改变: 对于范围内的i(1): 这改变了循环的数量: for i in range(5): 运行代码5次会有不同的结果,是否有代码可以找到5项试验中的最佳总分?
基本上我有什么可以用来总结4个评分,那么如果重复可以从所有重复中选出最佳总分吗?
答案 0 :(得分:1)
保持总计:
total = 0
for each_grp in my_groups:
c = get_rating(each_grp)
total += c
print(c)
您可以使用原始list
并使用生成器sum
向上len
set
:
>>> sum(get_rating(each_grp) for each_group in my_groups)
11